I am trying to move a character to the position of where the mouse was clicked. I tried using velocity, but it only moves until it’s velocity hits zero. How can I get a constant movement speed until the object reaches it’s destination?
Also, I want the character to move from 0 to max speed instantly and from max speed to 0 instantly as well.
Here is what I have tried:
void Update(){
int joysticks = Input.GetJoystickNames().Length;
// No Joysticks attached, use Mouse
if(joysticks < playerId){
if(Input.GetMouseButtonDown(1)){
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0f;
if (playerPlane.Raycast(ray, out hitdist)){
Vector3 targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
rigidbody.velocity = transform.forward * speed;
anim.SetBool("Run", true);
}
}else{
}
}