Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by FLASHDENMARK · Jun 06, 2011 at 03:43 PM · rotationquaternionsdamping

Smooth rotation on one axis only

Hello sexy.

In my third person game I have a few lines of code that looks in the cameras forward direction when the player moves forward and different directions when moving backwards and sideways.

 A small sample of code:
 (It is in the function Update()):


 if(Input.GetAxisRaw("Horizontal") > 0.1)
 {
 transform.eulerAngles.y = GameCam.eulerAngles.y + 90;
 transform.Translate(0,0,Input.GetAxisRaw("Horizontal")/20);
 }

Here the player moves right, and the player aligns himself with the camera(GameCam) + 90 degrees, but it happens in a instant and I would very much like it smoothed out/damped, but I have no general idea how to do so. I do know Quaternions would be of good use(I think), but I do not know how to implement it.

Any help received would be appreciated.

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
1
Best Answer

Answer by testure · Jun 06, 2011 at 06:20 PM

In addition to skail's answer, I'll point out that you need to convert your euler angles to a quaternion first:

to.rotation = Quaternion.Euler(new Vector3(angleX, angleY, angleZ)); Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);

this can obviously be combined into one line of code- but for clarity's sake i've broken it up into two parts.

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 FLASHDENMARK · Jun 06, 2011 at 08:04 PM 0
Share

Good point, It did not give any errors in the console, but the player dose not look in the camera direction whatsoever now. Weird -.-´

avatar image testure · Jun 06, 2011 at 08:13 PM 0
Share

if that's the case, your angles are probably not correct. If you're trying to make the player look at the camera, why not just use the transform.LookAt() method?

playerObject.transform.LookAt(Camera.mainCamera.transform);

If you needed it to interpolate you could even add some intermediate transform that gets interpolated, and look at that ins$$anonymous$$d.

avatar image skail · Jun 06, 2011 at 09:03 PM 0
Share

Try printing out both the rotations: print(from.rotation); print(to.rotation); $$anonymous$$aybe also keep track of its rotation while its trying to slerp its way around, I have had some issues with slerping and just printing out those thigs usually helps me figure out what is wrong.

avatar image
1

Answer by skail · Jun 06, 2011 at 04:23 PM

You could try using Quaternion.Slerp.

transform.rotation = Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);

Comment
Add comment · Show 2 · 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 FLASHDENMARK · Jun 06, 2011 at 04:47 PM 0
Share

So, true and thanks, but as said I only want on the Y axis and if you write it like(specificly tell it to rotate on the Y axis):

var turnSpeed = 0.1;

transform.rotation.y = Quaternion.Slerp (transform.rotation.y, GameCam.rotation.y, Time.time * turnSpeed);

It gives errors:

The best overload for the method `UNityEngine.Quaernion.Slerp(UnityEngine.Quaternion, UnityEngine.Quaternion, float)´ is not compatible with the argument list ´(float, float, float)´.

and:

Cannot convert ´UnityEngine.Quaternion to ´float´.

avatar image skail · Jun 06, 2011 at 05:55 PM 0
Share

Those errors are occurring because a normal transform.rotation value has 4 values and when you reference only 1 of them it causes problems. $$anonymous$$ost likely what you will need to do is make another transform.rotation value to reference and only modify its.y value.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I instantiate a gameobject facing another gameobject 2D? 0 Answers

local rotation per axis between frames? 1 Answer

How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer

Quaternions; how do I rotate an object around only 2 axis based on another rotation? 1 Answer

Get real compass direction 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