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
1
Question by ZorNiFieD · Dec 16, 2012 at 08:32 PM · c#rotationquaternion

rotate rigid body based on transform.velocity?

using c#, I've been searching for how to change the rotation to direction player is moving. I've found several solutions, but this looks the best:

 transform.rotation=Quaternion.SetLookRotation(velocity);

error CS0120: An object reference is required to access non-static member `UnityEngine.Quaternion.SetLookRotation(UnityEngine.Vector3)'

When I try using this code, i get the following errors:

I also try this with the same error:

 Quaternion myQ = new Quaternion.SetLookRotation(velocity);
 transform.rotation = myQ;

error CS0426: The nested type `SetLookRotation' does not exist in the type `UnityEngine.Quaternion'

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

Answer by clunk47 · Dec 16, 2012 at 08:51 PM

Not sure exactly what you mean but try this.

     void Update () 
     {
         transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
         print (transform.rotation);
     }

Otherwise, if you're wanting the object's rotation to only move on Y axis and act like a "turret"....

 using UnityEngine;
 using System.Collections;
 
 public class EXAMPLE : MonoBehaviour 
 {   
     public Transform player;
     Vector3 target;
     
     void Update () 
     {
         target = new Vector3(player.position.x, this.transform.position.y, player.position.z);
         transform.LookAt(target);
     }
 }
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 clunk47 · Dec 16, 2012 at 08:51 PM 1
Share

Sorry if this doesn't help, I'll need a more detailed explanation of what you're going for. Will be happy to help either way :)

avatar image ZorNiFieD · Dec 17, 2012 at 12:49 AM 1
Share

this helped a lot, thank you. I'm still new and learning so thank you for clarifying.

avatar image clunk47 · Dec 17, 2012 at 12:50 AM 1
Share

Always happy to help, I voted your question up +1 for being a good question :)

avatar image
0

Answer by aleksigron · Dec 17, 2012 at 01:11 AM

The reason you have these errors, is that SetLookRotation() of class Quaternion is a instance method, meaning you have to call it on an instance of that class. There is also a class method LookRotation() which does the same thing, you just have assign the return value somewhere.

So you can use

transform.rotation.SetLookRotation(velocity);

or

transform.rotation = Quaternion.LookRotation(velocity);

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

11 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

Related Questions

Flip over an object (smooth transition) 3 Answers

i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers

Making my GambeObject rotate to face the direction of movement, smoothly. 0 Answers

Why if I Rotate only 1 axis, it rotates the other 2 axis also? 2 Answers

OnDrawGizmos Debug.DrawLine position is pinching when rotated. 0 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