characterController reference issue
Hi, 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;
if(hittedObject==GameObject.FindGameObjectsWithTag("Ground")[0]){
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.
Your answer

Follow this Question
Related Questions
FPS Character Controller HELP with head bob and camera! 2 Answers
Why pressing the up and down key doesn't mean the body in correct forward and back direction? 1 Answer
Help please! Why can't I reduce the speed before jumping? 1 Answer
CharacterController.Move with slow animals - they do not move 1 Answer