• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by e-bonneville · Mar 28, 2010 at 03:18 PM · mouseclickrtswalking

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by duck · Mar 28, 2010 at 05:37 PM

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image e-bonneville · Mar 28, 2010 at 06:19 PM 0
Share

@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?

avatar image duck ♦♦ · Mar 28, 2010 at 08:40 PM 0
Share

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" ins$$anonymous$$d! (the .point property of a RaycastHit gives the world space location where the ray collided)

avatar image e-bonneville · Mar 28, 2010 at 08:52 PM 0
Share

@Duck duh. I'm so stupid... I just figured that out, without even looking at your comment. funny.

avatar image e-bonneville · Mar 28, 2010 at 08:53 PM 0
Share

@Duck Thanks, because if I didn't figure it out, your comment would have done the trick - it's the same thing.

avatar image e-bonneville · Mar 28, 2010 at 10:23 PM 0
Share

@Duck BTW, thanks for answering all my [many] questions! ;)

avatar image
0

Answer by e-bonneville · Mar 28, 2010 at 08:51 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image e-bonneville · Mar 28, 2010 at 08:53 PM 0
Share

I wrote this right before I looked at your comment.

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

RTS style mouse click and unit move to mouse click 2 Answers

VR autowalk function unity 5 3 Answers

How to make a character walk on a 3D cube planet and a long 3D cube planet using gravity? 2 Answers

Examples of TBS/Heavily Gui-driven Games in Unity? 2 Answers

RTS Style Selection system (How to control one unit and not the others) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges