Unity3D小球移動控制程式碼(向量相加)
阿新 • • 發佈:2018-12-26
using UnityEngine;
using System.Collections;
public class BallControl : MonoBehaviour
{
public float movementSpeed = 6.0f;//小球運動的速率
private Vector3 horizontalMovement;//小球的水平運動
//這裡理解為小球的前後運動
private Vector3 verticalMovement;
void Update()
{
horizontalMovement = Input.GetAxis("Horizontal") * Vector3.right * movementSpeed;
verticalMovement = Input.GetAxis("Vertical") * Vector3.forward * movementSpeed;
//小球的運動(水平運動與前後運動的向量和)
Vector3 movement = horizontalMovement + verticalMovement;
//為小球施加力
rigidbody.AddForce(movement, ForceMode.Force);
}
void OnTriggerEnter(Collider other)
{
//判斷小球是否與鑽石物件碰撞
if (other.tag == "Pickup")
{
//如果碰撞了,說明找到了鑽石
MarbleGameManager.SP.FoundGem();
CrazyBallManager.CB.FoundGem();
//銷燬物件
Destroy(other.gameObject);
}
else
{
//與其他物件的碰撞
}
}
}
using System.Collections;
public class BallControl : MonoBehaviour
{
public float movementSpeed = 6.0f;//小球運動的速率
private Vector3 horizontalMovement;//小球的水平運動
//這裡理解為小球的前後運動
private Vector3 verticalMovement;
void Update()
{
horizontalMovement = Input.GetAxis("Horizontal") * Vector3.right * movementSpeed;
verticalMovement = Input.GetAxis("Vertical") * Vector3.forward * movementSpeed;
//小球的運動(水平運動與前後運動的向量和)
Vector3 movement = horizontalMovement + verticalMovement;
//為小球施加力
rigidbody.AddForce(movement, ForceMode.Force);
}
void OnTriggerEnter(Collider other)
{
//判斷小球是否與鑽石物件碰撞
if (other.tag == "Pickup")
{
//如果碰撞了,說明找到了鑽石
MarbleGameManager.SP.FoundGem();
CrazyBallManager.CB.FoundGem();
//銷燬物件
Destroy(other.gameObject);
}
else
{
//與其他物件的碰撞
}
}
}