- Home /
Player moves too fast and won't move to point
I'm trying to make a point and click movement script but I can't seem to get the character to move to the point that my ray hits on the terrain, it moves towards it only when the mouse button is held down. Also the further away I click, the faster the character moves but I need the character to walk to where I click at a constant speed.
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
Ray ray = new Ray();
RaycastHit hit = new RaycastHit();
// Update is called once per frame
void Update () {
if(Input.GetButton("Fire1")){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast (ray, out hit, 100)){
Debug.Log ("Ray!");
transform.Translate((hit.point - transform.position) * Time.deltaTime);
}
}
}
}
Answer by Lovrenc · Jan 13, 2013 at 12:05 AM
You made 2 mistakes.
a) Player is only moving when i am holding down the button.
Yes, because you have your transform.Translate command inside the
if(Input.GetButton("Fire2"))
put it outside of that condition
b) Player speed
How you set it down now, you calculate the vector that tells you where to move. But this vector also contains the speed at which you are moving. Then you just multiplied that by Time.deltaTime. You have seen the result.
transform.Translate((hit.point - transform.position).normalized * playerSpeed * Time.deltaTime);
This should make your movement speed constant. Make sure to create playerSpeed variable od just put a constant number in its place intead.