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 3dDude · May 25, 2010 at 01:19 AM · 2dlookatsidescroll

2d follow script

hi i just started a 2d side scrolling shooter and i have a enemy that has a gun the points a the player. at first i just used LookAt but i wanted to have some dampening in the rotation so i used this code

function Update () { var target = transform.root.gameObject.GetComponent("AI").target; var rotate = Quaternion.LookRotation(target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * smooth); }

but i get this weird error ware if i move to close it starts to rotate so that its not in 2d. please help

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
3

Answer by Mike 3 · May 25, 2010 at 03:45 PM

The easiest way is to just set one axis to 0 in the vector you pass to LookRotation.

If your game is constrained to the XY plane, that axis will be Z, etc.

Example:

function Update () { var target = transform.root.gameObject.GetComponent("AI").target; var offset = target.position - transform.position; offset.z = 0; var rotate = Quaternion.LookRotation(offset);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * smooth); }

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 3dDude · May 25, 2010 at 04:01 PM 0
Share

hhhhhmmmmmm it still does the same thing it did before

avatar image Mike 3 · May 25, 2010 at 04:26 PM 0
Share

$$anonymous$$ake sure you're clipping off the right axis (Try them all just in case)

avatar image 3dDude · May 25, 2010 at 06:52 PM 0
Share

that did not work ether

avatar image Eric5h5 · May 25, 2010 at 07:11 PM 1
Share

That code does in fact work fine.

avatar image gorogorosama7 · Feb 23, 2014 at 06:21 PM 0
Share

I'm also having troubles. When I just set the rotation to face the target, it works fine.

  var offset = myTarget.position - transform.position;
  offset.z = 0;
  transform.rotation= Quaternion.LookRotation(offset);

However, when I try to use the Quaternion.Slerp as suggested to smooth the turning, they start spinning around on all axis again... Thoughts

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

1 Person is following this question.

avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

LookAt to control view angle stuck at 180 degrees rotation 1 Answer

BOX ROTATE ONLY ON X AXIS When Look AT!!HELP! 0 Answers

2d look at workaround creates jitter 0 Answers

Making a 2d object lookat a 3d object on the y-axis(c#) 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