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 W1k3 · Jan 23, 2014 at 10:18 PM · rotationquaternionlookatslerpmotion

Look at rotation at a moving object while moving (C#)(2D)

I used some javascript that happened to work in C#.

             var newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.back);
         
             newRotation.x = 0.0f;
 
             newRotation.y = 0.0f;
         
             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);

It's attached to an enemy space ship that follows you by turning to face you and moving forward. It works fine if the enemy is stationary and only the player moves, but when they both move the rotation jitters and doesn't work properly at all. If I turn the rotation speed up to the point of instantaneous, then it works fine, but in this game I need it to be a slow and smooth rotation.

Any Idea on how to fix this, or maybe an alternative way of rotating on a 2D axis would be awesome.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryGenius · Jan 23, 2014 at 10:50 PM

One problem is that you're editing individual components of a quaternion, which is really complicated, so you should probably convert the above code to something more like

 Quaternion newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.back);
      Vector3 eulers = newRotation.eulerAngles;
              eulers.x = 0.0f;
      
              eulers.y = 0.0f;
      nwRotation = Quaternion.Euler(eulers);
              transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);

And then move that code to LateUpdate or FixedUpdate if that doesn't fix it.

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 W1k3 · Jan 23, 2014 at 11:06 PM 0
Share

Right now they will only look left and right. If I'm on the left side, they just snap and face straight to the left with no in between. Late update didn't make a difference. I also changed nwRotation to newRotation because it looks like a typo.

avatar image
0

Answer by venhip · Jan 23, 2014 at 11:39 PM

Hey W1k3,

I'm working on a 2D game at the moment and this is the bit in my scripts I use for looking at another object:

 rigidbody2D.transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((TARGETTRANSFORM.position.y - transform.position.y), (TARGETTRANSFORM.position.x - transform.position.x))*Mathf.Rad2Deg - 90);

I adapted this from a script that causes the object the script is on to look at the mouse. If this doesn't work let me know and I'll try to provide a better explanation, but this should work if you replace "TARGETTRANSFORM" with a transform of whatever name you want.

I program in C# but when I tested this with Javascript it didn't throw any errors, so I hope it's fine for you.

Good luck.

P.S. Put this in your FixedUpdate function or wherever your previous rotation script was.

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 W1k3 · Jan 24, 2014 at 01:13 AM 0
Share

Thanks for the Reply. This rotates to face my player well, but do you know if there is a way to modify the speed in which it rotates? Also, if the enemy get's hit by a projectile, it causes it to rotate a tiny bit from the force. After that the enemy doesn't face the player right on, always a bit to the left or the right. After a while it begins to face the player again, but it's a bit of nuisance. I'm using C# by the way.

avatar image
0

Answer by robertbu · Jan 23, 2014 at 11:41 PM

Try this instead:

      var dir = transform.position - target.position;
      var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
      var newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
      transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);

This assumes that this object is a sprite or Quad in which the 'forward' is to the right. If the forward is 'up', then you will need to adjust the angle by 90 degrees after the Atan2() call.

Comment
Add comment · 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

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

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

How can I get the rotation between two objects 1 Answer

Gameobject Rotation Calculation 0 Answers

Interpolate rotation of character. 0 Answers

Quaternion.Slerp 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