- Home /
Move to clicked hitpoint
I have a problem with click to move script. I use now this:
var speed = 1;
var targetRadius = 0;
private var target : Vector3;
var hit : RaycastHit; //added
function Start () {
rigidbody.freezeRotation = true;
target = transform.position;
}
function FixedUpdate () {
if (Input.GetButtonDown ("Fire1")) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
animation.CrossFade("walk"); //added
target = hit.point;
}
}
if (Vector3.Distance(transform.position, target) > targetRadius) {
transform.LookAt(target);
transform.Translate(0,0,speed);
target.y = transform.position.y;
}
}
But its doing very weird, when i start my game the character is bouncing and doing strange moves... And than the character is under the terrain.
Answer by AlucardJay · Jul 06, 2012 at 06:21 PM
try swapping target = hit.point;
with (untested) :
target = new Vector3( hit.point.x, transform.position.y, hit.point.z);
Debug.Log("hit Y = " + hit.point.y + " : transform Y = " + transform.position.y);
in the console you should see a printout of where the mouse ray hit position y is, and the transform's y position.
did this help? comments and/or accepting answers on this 'site is appreciated and helps others searching this 'site. Support the 'site that has supported you.
Answer by rikkert88 · Jul 06, 2012 at 04:39 PM
anyone?? ????????
hi, please don't post comments as answers. Use the [add new comment] button, a window then pops up. you can delete this answer, and comment on the answer I left =]