x


How to use the LookAt function

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.

var MouseClick : MouseClick;
var speed = 2;

function unitSelection () {
    MouseClick.selected = true; 
    Debug.Log ("I'm selected. What can I do for you?");
}

function Update() {
    if (MouseClick.move) {      
        transform.LookAt(MouseClick.TargetDestination);         
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}

How can I make this happen?

EDIT: Here lies the MouseClick script:

var selected = false;
var move = false;
var TargetDestination;

function Update () {
    if (Input.GetButtonDown ("Fire1")) {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) {
            if (hit.collider.tag == "Unit") {
                    hit.collider.SendMessageUpwards ("unitSelection");

            }
            else if (hit.collider.tag == "Terrain") {
                Debug.Log ("You Missed!");
            }
        }
    }

    if (selected && Input.GetButtonDown ("Fire1")) {        
        var ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit1 : RaycastHit;
        if (Physics.Raycast (ray1, hit1)) {
            if (hit1.collider.tag == "Unit") {
                    Debug.Log ("I'm already selected!");
            }
            else if (hit1.collider.tag == "Terrain") {
                selected = false;
                Debug.Log ("You unselected!");
            }
        }
    }

    if (Input.GetButtonDown ("Fire2") && selected) {
        var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit2 : RaycastHit;

        if (Physics.Raycast (ray2, hit2)) {
            if (hit2.collider.tag == "Unit") {
                    Debug.Log ("I'm already selected!");
            }
            else if (hit2.collider.tag == "Terrain") {
                Debug.Log ("I'm going already!");
                move = true;
                TargetDestination = Input.mousePosition;                
            }
        }
    }
}

Thanks in advance - Elliot Bonneville.

more ▼

asked Mar 28 '10 at 03:18 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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:

var lookTarget = MouseClick.TargetDestination;
lookTarget.y = transform.position.y;
transform.LookAt(lookTarget);         

hope this helps!

more ▼

answered Mar 28 '10 at 05:37 PM

duck gravatar image

duck ♦♦
41k 92 148 415

@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)
10|3000 characters needed characters left

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.

more ▼

answered Mar 28 '10 at 08:51 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

I wrote this right before I looked at your comment.

Mar 28 '10 at 08:53 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x155
x88
x67

asked: Mar 28 '10 at 03:18 PM

Seen: 6127 times

Last Updated: Mar 28 '10 at 06:18 PM