• 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
24
Question by AwesomeFaceHD · Jan 14, 2014 at 04:33 AM · c#rotationmovementdirection

How to make an object go the direction it is facing?

I have a cube that is my player and I can move it with WASD. I can also rotate it with QE. When I rotate it though, it still goes the the same direction when I press a directional button. It doesn't matter what direction it is facing. So how can I make it always go forward based on the front of the cube? Any help would be much appreciated.

Script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(Rigidbody))]
 public class Movement : MonoBehaviour {
 
     public float movementSpeed = 5.0f;
     public float clockwise = 1000.0f;
     public float counterClockwise = -5.0f;
 
     void Start () {
         
     }
     
     void Update () {
         if(Input.GetKey(KeyCode.W)) {
             transform.position += Vector3.forward * Time.deltaTime * movementSpeed;
         }
         else if(Input.GetKey(KeyCode.S)) {
             rigidbody.position += Vector3.back * Time.deltaTime * movementSpeed;
         }
         else if(Input.GetKey(KeyCode.A)) {
             rigidbody.position += Vector3.left * Time.deltaTime * movementSpeed;
         }
         else if(Input.GetKey(KeyCode.D)) {
             rigidbody.position += Vector3.right * Time.deltaTime * movementSpeed;
         }
 
         if(Input.GetKey(KeyCode.E)) {
             transform.Rotate(0, Time.deltaTime * clockwise, 0);
         }
         else if(Input.GetKey(KeyCode.Q)) {
             transform.Rotate(0, Time.deltaTime * counterClockwise, 0);
         }
     }
 }
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

6 Replies

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

Answer by GameVortex · Jan 14, 2014 at 07:35 AM

Vector3.forward and the others are in world space, so they will always move you in the same direction no matter which direction your character is in. To fix this you can replace the Vector3.forward with transform.forward. transform.forward is the forward direction of the transform, taking rotation into account.

So you change this:

 transform.position += Vector3.forward * Time.deltaTime * movementSpeed;

Into this:

 transform.position += transform.forward * Time.deltaTime * movementSpeed;

Transform does not have the variables left and back though, so you use the negative version of right and forward instead. Or simply subtract movement instead of adding it.

Move Back:

 transform.position -= transform.forward * Time.deltaTime * movementSpeed;
Comment
Add comment · Show 14 · 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 AwesomeFaceHD · Jan 14, 2014 at 05:06 PM 0
Share

Sorry for late response. This worked perfectly. Thank you.

avatar image morbidcamel · Jan 14, 2014 at 06:29 PM 0
Share

But you said your using a rigid body??

avatar image jdceddia · Mar 14, 2015 at 11:50 AM 2
Share

i love you

avatar image macrian · Apr 11, 2016 at 07:38 AM 0
Share

I can only say thank you

avatar image juli303 · May 25, 2016 at 08:23 AM 0
Share

$$anonymous$$aybe you can help me too. I'm making a 2D game right now and this was my first script for a bullet: movement_vector = new Vector2(1, 0); rbody.$$anonymous$$ovePosition(rbody.position + movement_vector * Time.deltaTime * Speed); So now I'm trying to make it move into the direction that it's facing: Attempt 1: rbody.$$anonymous$$ovePosition((Vector2)transform.forward * Time.deltaTime * Speed); Attempt 2: rbody.position += (Vector2)transform.forward * Time.deltaTime * Speed; I'm sorry if my question seems extremely stupid as I'm relatively new to unity. Neither of my attempts seem to work as the bullet isn't moving. $$anonymous$$aybe someone can help me?

Edit: Fixed it

 transform.Translate(transform.right * Speed * Time.smoothDeltaTime);




avatar image GameVortex juli303 · May 25, 2016 at 08:41 AM 1
Share

You are casting a Vector3 (transform.forward) into a Vector2 which basically just removes the Z value of the Vector3. In 2D you cannot move forward because forward is in the direction of the Z Axis which is into the screen. So in 2D you want to use the UP or Right direction ins$$anonymous$$d: transform.up. In 2D the Up and Right direction will always have a Z value of 0 which means that it can safely be ignored and passed into the $$anonymous$$ovePosition function.

If you are new to Unity and want to make a 2D game I recommend you to start with some of the 2D tutorials which covers many of these things: https://unity3d.com/learn/tutorials/topics/2d-game-creation

It is also generally better to ask a new question ins$$anonymous$$d of commenting on an answer on another if you need help. Also remember to search for your issue, searching for "Unity 2D Forward" came up with this helpful answer: http://answers.unity3d.com/questions/797202/finding-forward-in-2d-rigid-body.html

Show more comments
avatar image
12

Answer by arul20 · Oct 23, 2017 at 08:20 AM

Use AddRelativeForce - which adds a force to the rigidbody relative to its / local coordinate system, rather than AddForce which adds based on the world's coordinate system.


code solution / sample


alt text


E.g: If the spaceship is rotated away from the station, pressing forward with AddRelativeForce will use the ship's position and rotation, moving the ship forward in the correct direction. AddForce will use the station's / world's coordinates and ignore the ship's rotation - so when you press forward, the ship will move backward, towards the station.


screen-shot-2017-10-23-at-41538-pm.png (162.7 kB)
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
4

Answer by morbidcamel · Jan 14, 2014 at 05:05 AM

If you're movement is physics based you can calculate the angular velocity... I use this in movement of some robot characters and needed more than just a simple Lerp. Here's a typical rigidbody movement motor.. Assuming you have 2 vectors, point a the position of the game object, b the position of the target to move to...

 // First calculate the force - ...
 ...
 Vector3 nextDirection = (b - a).normalized;                       
 Vector3 targetVelocity = nextDirection * speed;
 Vector3 deltaVelocity = targetVelocity - thisRigidbody.velocity;
                                  if (thisRigidbody.useGravity)
                                         deltaVelocity.y = 0;
 
  force = deltaVelocity * accelerationRate;
 // Then we can calculate the angular velocity
 
                 // Make the character rotate towards the target rotation
                 var turnDir = nextDirection;
                 if (turnDir == Vector3.zero) 
                 {
                         angularVelocity = Vector3.zero;
                 }
                 else 
                 {
                         var rotationAngle = AngleAroundAxis (transform.forward, turnDir, Vector3.up);
                         angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
                 }

Then you can set the rigidbody --

 thisRigidbody.AddForce (force, ForceMode.Acceleration);
 thisRigidbody.angularVelocity = angularVelocity;

Here is how to calculate the angle around an axis (make it turn around its head y axis)

 public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis) 
                 {
                     // Project A and B onto the plane orthogonal target axis
                     dirA = dirA - Vector3.Project (dirA, axis);
                     dirB = dirB - Vector3.Project (dirB, axis);
 
                     // Find (positive) angle between A and B
                     float angle = Vector3.Angle (dirA, dirB);
 
                         // Return angle multiplied with 1 or -1
                     return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
                 }

To make sure it doesn't overshoot make use of a in range calculation and the adjust the speed for the component above. You can do this with ( b - a).magnitude or Vector3.Distance. The trick is to calculate the gradual deceleration or you can just make it stop with a speed = 0. Let me know if you need more help.

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
avatar image
1

Answer by hari2015 · Feb 12, 2018 at 06:02 PM

transform.Translate(Vector3.forward*speed*time.dealtatime);

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
avatar image
1

Answer by NFad · Apr 27, 2018 at 05:53 AM

I would certainly advise against setting (adding in this case) the position every time you move the player. Please either use transform.Translate() as the replies above suggest, or go all the way and add a rigidbody and use GetComponent().AddForce(Vector3.forward Time.deltaTime movementSpeed) in order to also give unity the power to also calculate collisions (if you have a collider attached) and physics if necessary.

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
  • 1
  • 2
  • ›

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

44 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to rotate a camera in WorldSpace CORRECTLY 0 Answers

How do I turn 1 objects rotation into another objects movement direction? 1 Answer

3D Heat Seeking Missile (C#) - Odd problem 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