Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Zepeck · Mar 19, 2014 at 09:30 PM · spritevector3lookatfollowlocation

I can get a sprite to look at my mouse but not another Vector3, im not doing anything wrong but the sprite just looks in one direction.

I want a sprite to follow my player and i found some code to follow my mouse and the sprite looked at it wherever he was, however, when i changed out the variable for the mouse position to t, he vector3 storing my players location it looks in one direction, not even a point. I dont knwo what im doing wrong. Here are the two bits of code, firstly the one that tracks my mouse.

     mousePosition = Input.mousePosition;            

     mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

     //transform.LookAt(new Vector3(mousePosition.x, mousePosition.y, transform.position.x)); <---- THIS LINE WAS COMMENTED OUT BUT IT WORKS EITHER WAY

     Quaternion rot = Quaternion.LookRotation( transform.position - mousePosition, Vector3.forward );

     transform.rotation = rot;   



And here is the edited code for following my sprite.

 var mousePosition = shipLoc;           
 mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

 transform.LookAt(new Vector3(heroX, heroY, -10)); 

 var rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward );

 transform.rotation = rot;   
 transform.eulerAngles = new Vector3(0, 0,transform.eulerAngles.z);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 19, 2014 at 09:33 PM

Assuming 'shipLoc' is a world position, you need to delete the second line in your second code fragment. ScreenToWorldPoint() translates the screen pixels into a world position. You don't need that if 'shipLoc' is a world coordinate.

Comment
Add comment · Show 3 · 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 Zepeck · Mar 19, 2014 at 09:37 PM 0
Share

@robertbu Deleted that line and the enemy stares in a different direction but still doesnt follow me. :(

avatar image robertbu · Mar 19, 2014 at 10:47 PM 0
Share

'shipLoc must be a Vector3? If so, how do you update its value. A better solution would be to make 'shipLoc' a Transform. You would initialize it through drag and drop. Then you would do this:

  var mousePosition = shipLoc.position;

Vector3s are structs not classes. You access them by value ins$$anonymous$$d of by reference. That means that 'shipLoc' is not being updates as you move.

avatar image Zepeck · Mar 20, 2014 at 04:42 PM 0
Share

I tried this, and now my enemy will only look left or right.

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera follow 2D sprite when it goes off screen 1 Answer

LookAt To Only Rotate on Y Axis 2 Answers

Can anyone please tell me why this script doesnt work? - Raycasting 1 Answer

Camera Follow Object While Looking At Other Target 2 Answers

My AI code is only making a sprite look Left or Right? Help please. 1 Answer


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