|
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.
(comments are locked)
|
|
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! @Duck I added the mouseclick script for you. Am I right in thinking that I can use the mouseinput as a Vector3 on contact with an object?
Mar 28 '10 at 06:19 PM
e.bonneville
Yes, you can't directly use the mousePosition as a 'lookAt' target. You'll need to convert it to a world position. However in your case, this may be as simple as using "hit2.point" instead! (the .point property of a RaycastHit gives the world space location where the ray collided)
Mar 28 '10 at 08:40 PM
duck ♦♦
@Duck duh. I'm so stupid... I just figured that out, without even looking at your comment. funny.
Mar 28 '10 at 08:52 PM
e.bonneville
@Duck Thanks, because if I didn't figure it out, your comment would have done the trick - it's the same thing.
Mar 28 '10 at 08:53 PM
e.bonneville
@Duck BTW, thanks for answering all my [many] questions! ;)
Mar 28 '10 at 10:23 PM
e.bonneville
(comments are locked)
|
|
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. I wrote this right before I looked at your comment.
Mar 28 '10 at 08:53 PM
e.bonneville
(comments are locked)
|
