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 /
  • Help Room /
avatar image
0
Question by MudPuppet · Jan 26, 2017 at 07:54 AM · rotationrigidbodyvelocity

Rotate GameObject based on RigidBody Velocity

Hello,

Just wondering if someone would be kind enough to help me work something out (in C# preferably).

Basically I have a cube with a little propeller on the top of it. The cube has a Rigidbody and is controlled by keys (for forwards/backwards motion). I am trying to work out how to make the propeller rotate (clockwise/anti-clockwise on Y axis) based on the RigidBody’s Velocity.

Ideally I am wanting to be able to drag the object with the RigidBody component and the propellor GameObject onto the script in the inspector.

I have a ‘faked’ solution where it rotates the propellor based on key input and a float for rotation speed but as soon as I am not holding down a key it stops rotating so it looks a bit silly to see the cube slowing down and the propellor has already stopped rotating.

Any help would be very much appreciated.

Comment
Add comment · Show 1
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 MudPuppet · Feb 11, 2017 at 09:26 AM 0
Share

Problem Solved! Didn't need to use velocity at all. $$anonymous$$uch thanks to RuneFire Studio for their assistance.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MudPuppet · Feb 11, 2017 at 09:25 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerRotObject : MonoBehaviour 
 {
 
     // SPEED = DISTANCE / TIME
 
     public Vector3 localV;
 
     public float RBVelo;
 
     public float WheelRadius=2.35f; 
 
     public GameObject thisObject;     // Object with Rigidbody
     public GameObject leftProp;        // Left Propellor Object
     public GameObject rightProp;    // Right Propellor Object
     private Rigidbody thisRB;
 
     Vector3 currentPosition;
 
     // Use this for initialization
     void Start () 
     {
         
         thisRB = thisObject.GetComponent<Rigidbody>();
 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         RBVelo = thisRB.velocity.magnitude;
 
         Vector3 localVelocity = transform.InverseTransformDirection( thisRB.velocity );
         // Just to visualize in the inspector
         localV = localVelocity;
 
         float dir_mult = 1; // Determine direction to "spin" the wheel; 1 is forward, -1 is backward
         if(localVelocity.z < 0) dir_mult = -1;
 
         // Magnitude * time.deltaTime is basically the "distance" the rb traveled
         //  So, we need to figure out how much to "spin" the wheels...
         // this ONLY takes into account SPEED, not DIRECTION.  So, if hte car is moving backwards,
         // we'd need to actually multiply by -1
         float degRot = ( (RBVelo * Time.deltaTime) / WheelRadius) * Mathf.Rad2Deg * dir_mult;
         leftProp.transform.Rotate( degRot, 0, 0 );
         rightProp.transform.Rotate( degRot, 0, 0 );
 
     }
 }
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

114 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 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 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 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 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 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

Storing instantaneous velocity 0 Answers

How to get rigidbody velocity? 3 Answers

Rigid body velocity rotation,Rotate instead of Velocity? 1 Answer

Torque rigidbody toward desired rotation on two axes ignoring Y axes? 0 Answers

Error CS1612 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