I am trying to move my character to point that taken from mouse position.

here is my code:

private Vector3 hittedPoint=Vector3.zero;

 if (Input.GetMouseButtonDown (0)) {
             Ray ray = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;

         if (Physics.Raycast(ray, out hit)) {
             //get object
             GameObject hittedObject=hit.transform.gameObject;


                 hittedPoint= new Vector3(hit.point.x,transform.position.y,hit.point.z);
                 //hittedPoint = transform.TransformDirection (hittedPoint);


             if (hittedPoint != Vector3.zero) {
                 float delta = (transform.position - hittedPoint).magnitude;
                 if (delta > 5) {
             controller.Move (hittedPoint*Time.deltaTime);
             Debug.Log (transform.position);
                 } else {
                     hittedPoint = Vector3.zero;

The problem is that my object moves according to its first position at the scence. For example if I move character towards right then towards forward; at second movement it goes diagonal. It is like it makes its movements according the first position of it in scence.

The second problem is; i couldn't find efficient method for stopping movement when character reaches the point. If I decrease delta; character skips the point and if I increase it, character stops to early.



controller.Move(( hittedPoint - transform.position)*speed*Time.deltaTime);

to solve the two problems. Speed is for second problem. Decrese it as you get closer to target.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.