How would I use the LookAt function? For example, I have a character that I control by clicking on a specific point on the ground. I have selection and walking working (sort of), but I have two problems. First up: I'm using the LookAt function to look at a point at the ground and then walk over there. However, the LookAt doesn't work in reverse. If I click behind the unit, it won't turn around. Second, I would like to stop it from rotating up and down, so the head is always horizontal to the ground. Here's my script currently. The variables referenced are in another script that gets the mouse input.
How can I make this happen?
EDIT: Here lies the MouseClick script:
Thanks in advance - Elliot Bonneville.
Well, "LookAt" itself does work in any direction, so I'm guessing your problem probably lies somwehere in your "MouseClick" class. Perhaps it doesn't contain the values that you think it does.
Since "MouseClick" seems to be your own custom class, and you don't show the code which populates its "TargetDestination" variable, it's difficult to guess any further about where the problem lies.
However, the second part of your question - how to stop it from looking up or down and instead have it look parallel to the ground - is relatively straightforward.
First, put your target "LookAt" position into a Vector3 variable, then set the y component of that Vector3 variable to the character's own Y value. You then have a target which differs only in the X and Z plane. Eg:
hope this helps!
answered Mar 28 '10 at 05:37 PM
Hah! Got it. Apparently, I put the mouse's screen coordinates in MouseClick.TargetDestination, when I actually needed the mouse's ray coordinates. Now it works! Yay.
answered Mar 28 '10 at 08:51 PM