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 JPB18 · Jul 28, 2012 at 07:22 PM · rotationjavascriptmovementvelocityvectors

Make object move in the direction is facing when rotated

Hi there!

I've this problem where I manage to add the velocity to the game object. However, once I try to change it's direction (using rotate), the object just keeps the previous momentum, without updating to the current direction.

So, how can I make my object change its vector direction every time I rotate it?

Here's the current code:

 //<---------------movement------------------------------>
 //these are to be replaced through stored variables
 var agility : float = 90.0; //Agility of the craft, soon to be controled through stored variables. In degrees per second.
 var speed : float = 10.0; //Speed of the craft, soon to be controled through stored variables
 
 //frame dependent vars
 var amountToMove : float; //amount of the movement 
 var agilityFrame : float; //amount of rotation
 var frameMulti : float = 20; //controls the movement multiplier (higher values mean the action occurs faster)
 
 //control vars
 var curSpeed : float; //this one checks the object current speed
 var wheelStatus : float; //this one controls the state of the mouse wheel, between 0 and 1;
 //this function controls the ship movement
 function movement() {
  agilityFrame = agility * Time.deltaTime * frameMulti;
  
  //Movement
  //obtain movement variables
  var vert = Input.GetAxis("Vertical") * agilityFrame;
  var horz = Input.GetAxis("Horizontal") * agilityFrame;
  var rot = Input.GetAxis("Rotate") * agilityFrame;
  
  
  //here we rotate the ship
  transform.Rotate(vert,horz,rot);
  
  
  //obtain speed variables
  amountToMove = speed * Time.deltaTime * 20;
  if (wheelStatus < 1 && wheelStatus > -1)
  {
  wheelStatus += Input.GetAxis("ShipSpeed");
  }
  if(wheelStatus > 1)
  {
  wheelStatus = 1;
  }
  if(wheelStatus < -1)
  {
  wheelStatus = -1;
  }
  fwd = wheelStatus *  amountToMove;
  
  
  //give speed to the ship
  
  rigidbody.velocity.z = fwd;
  
  
  
  
  
  //place current velocity into control variable
  curSpeed = rigidbody.velocity.magnitude;
  
  //make sure that current speed isn't bigger that maximum speed or smaller than minimum speed
  if (curSpeed > speed)
  {
  rigidbody.velocity.z = speed;
  
  }
  if (curSpeed < -speed)
  {
  rigidbody.velocity.z = -speed;
  
  }
  
  //reassign current velocity into control variable
  curSpeed = rigidbody.velocity.z;
 
 }

So, how can I make this work?

I appreciate any answers. JPB18

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

Answer by ScroodgeM · Jul 28, 2012 at 07:27 PM

rigidbody.velocity is world-based speed

you should use

http://docs.unity3d.com/Documentation/ScriptReference/Transform.InverseTransformDirection.html

this to convert world to local speed. example:

rigidbody.velocity = transform.InverseTransformDirection(Vector3.forward) * speed;

or

rigidbody.velocity = transform.forward * speed;
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 JPB18 · Jul 28, 2012 at 07:40 PM 0
Share

Thank you for your answer, however I'd like to know one thing: How can I know the speed of the object (independently of the direction its going)? I'm gonna need it for some other parts of the game, like the damaged caused by a collision, p.e..

avatar image ScroodgeM · Jul 28, 2012 at 07:49 PM 1
Share

object's speed in world space:

rigidbody.velocity
object's speed in local space:

transform.InverseTransformDirection(rigidbody.velocity);
object's speed in local forward direction:

Vector3.Dot(transform.forward, rigidbody.velocity);
avatar image JPB18 · Jul 28, 2012 at 07:53 PM 0
Share

Thank you very much. Tagged as answer now.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object doesn't turn at the desired rate 1 Answer

How to make bullets moving proportionally faster than player? 4 Answers

Moving a wheel character 0 Answers

Move the object down while it rotates 1 Answer

Stop A player Turning at Specific Point. 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