Question by
blanktarget · Aug 27, 2017 at 11:45 PM ·
2draycast2d game2d-physics2d animation
2d object moves only a little then stops
So I'm working in 2D and trying to move a gameobject on tap or mouse click to that position. Right now when I click the gameobject stutters a tiny step toward my mouse but stops almost instantly. Below is the movement code I made. What am I missing here?
private Vector3 target;
public float speed;
public Camera myCam;
Animator anim;
void Start(){
anim = GetComponent<Animator> ();
target = transform.position;
speed = 1.5f;
}
void Update() {
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) {
//if in the editor take mouse controls
#if UNITY_EDITOR
target = myCam.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
//else if it's on device
#else
target = myCam.ScreenToWorldPoint(Input.Touch);
#endif
//now move it move it.
anim.SetBool ("isWalking", true);
transform.position = Vector3.MoveTowards (transform.position, target, speed *Time.deltaTime);
}
}
Comment