Good day, I want to move my Rigidbody2D object forward at constant speed and change it direction when user click on screen. I have code:
public float speed;
void FixedUpdate()
{
if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
if(Input.touchCount > 0) {
if(Input.GetTouch(0).phase == TouchPhase.Began){
}
}
} else {
if(Input.GetMouseButtonDown(0)) {
var touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (transform.position - touchPosition, Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z); // only z rotation
myScriptRigidBody.velocity = (touchPosition - transform.position).normalized * speed;
}
}
}
As I understand, the object should change it's direction to mouse click position and move at constant speed. But on practise the object's speed depends on how far from current object position user clicked on screen. It is strange, bcs I normalized my direction vector and "speed" var is constant. What am I doing wrong? Thx.