- Home /
Click to move script help
I don't really get what I'm doing wrong. I'm new to scripting and am determined not to just use packages, as I do want to learn to script. There are no error messages and the x, y and z variables of dest don't change when I click. Could someone give me a rundown of what I'm missing please?
pragma strict
var dest : Vector3;
function Update () { if(Input.GetKey(KeyCode.Mouse0)){
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction*10,Color.yellow);
dest = hit.point;
Vector3.MoveTowards(transform.position,dest,10*Time.deltaTime);
} }
Answer by Sospitas · Jul 29, 2013 at 04:07 PM
I think you are missing using an actual raycast. You are creating the ray, however you are not casting it out and calculating the hit point.
Add in the following after the line which starts var ray:
Physics.Raycast(ray, out hit);
That should hopefully solve your problems. If not, post again and I'll see if I can find out some more :)
Thanks for the help. I used Physics.Raycast(ray, hit); (not sure if you need "out" for non-javascript) and it's got the dest variables updating. However the object is not moving towards the point. have I done something wrong with the $$anonymous$$oveTowards?
I work mainly in C#, so you might be right in saying that javascript does not need the out.
For the movement, try changing the
Vector3.$$anonymous$$oveTowards(transform.position,dest,10*Time.deltaTime);
to
transform.position = Vector3.$$anonymous$$oveTowards(transform.position,dest,10*Time.deltaTime);
Thank you so much! That works perfectly and I even sort of understand the way it works. Thanks again for the help!
Your answer
Follow this Question
Related Questions
Raycast Coding Issues 1 Answer
Using raycast - Why can't I get a variable on the object I hit? 1 Answer
Raycast Hit not toggling variable if stops hitting 1 Answer
Why multiply a vector by -2? 1 Answer
Stop the lasers from keep bouncing 1 Answer