我有一个连续跳动的球,我想用手指在x轴上左右移动它,所以它跟随我手指的X移动。
我把脚本放到了下面,但是球没有用手指移动!
球形脚本:
private float deltaX;
private Vector3 pos;
public Camera cam;
public Rigidbody Ball;
public float Speed;
public static float GlobalGravity = -9.8f;
public float GravityScale = 1.0f;
bool isforce = false;
private void Start(){
Ball = GetComponent<Rigidbody>();
Ball.useGravity = false;
}
private void FixedUpdate(){
Vector3 gravity = GlobalGravity * GravityScale * Vector3.up;
Ball.AddForce(gravity, ForceMode.Acceleration);
}
void force (){
isforce = false;
Ball.AddForce(Vector3.up * Speed, ForceMode.Impulse);
}
private void Update(){
if (isforce == true){
force();
}
if(Input.touchCount > 0){
Touch touch = Input.GetTouch(0);
Vector3 touchPos = cam.ScreenToWorldPoint(touch.position);
switch (touch.phase){
case TouchPhase.Began:
deltaX = touchPos.x - transform.position.x;
break;
case TouchPhase.Moved:
Ball.MovePosition(new Vector3(touchPos.x - deltaX, transform.position.y, transform.position.z));
break;
case TouchPhase.Ended:
Ball.velocity = Vector3.zero;
break;
}
}
}
private void OnCollisionEnter(Collision collision){
isforce = true;
}
您有什么建议吗?