• 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
2
Question by KoalaBearries · May 26, 2017 at 04:51 AM · rotationmovementtransformpositionplayer

make player move in direction it's facing

Hello to all, currently I am having issues making my player move in the direction of its rotation. For some reason it only wants to move in the first direction it was facing. I would like it to continue to only move by hoping. Thanks in advance!

 public GameObject player;
 public float speed = 7.0f;
 public float rotateSpeed = 3.0f;
 

 void Update()
 {     
     //Moves player upwards
     if (Input.GetAxis("Vertical") == 1)
     {
         var move = new Vector3(0, Input.GetAxis("Vertical"), 0);
         transform.Translate(transform.up * speed * Time.smoothDeltaTime);
     }

     //Rotates player direction
     if (Input.GetAxis("HorizontalTurn") > 0)
         transform.Rotate(Vector3.up * 90 * Time.deltaTime);

     else if (Input.GetAxis("HorizontalTurn") < 0)
         transform.Rotate(Vector3.up * -90 * Time.deltaTime);

     //launches player forward based on momentum from left stick and facing direction of right
     if (Input.GetAxis("rightTrigger") == 1 && Input.GetAxis("Vertical") == 1)
          {
               transform.position += Vector3.forward * Time.deltaTime * speed;
          }
 } 

}

Comment
Add comment · Show 2
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 Umresh · May 26, 2017 at 05:23 AM 0
Share

Use transform.forward to get the direction player is facing

avatar image Soujiro-Senpai · May 26, 2017 at 05:52 AM 0
Share

Why don't you use Rigidbody?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Soujiro-Senpai · May 26, 2017 at 04:18 PM

Use Rigidbody to make it easy:

 public float movementSpeed = 10f;
 Vector3 movement;
 
 Rigidbody rigidbody;
 
 void Awake()
 {
     rigidbody = GetComponent<Rigidbody>();
 }
 
 void FixedUpdate()
 {
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Horizontal");
 
     MoveCharacter(horizontal, vertical);
 }
 
 void MoveCharacter(horizontal, vertical)
 {
     movement.Set(horizontal, 0, vertical);
     if (horizontal != 0 || vertical != 0)
     {
         rigidbody.MoveRotation(Quaternion.LookRotation(movement));
     }
     movement = movement.normalized * movementSpeed * Time.deltaTime;
     rigidbody.MovePosition(transform.position + movement)
 }

That's the code if I remember it right. Haha

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
0

Answer by R4mbi · May 26, 2017 at 09:56 AM

You should move your player with a Rigidbody.
transform.Translate is more like teleportation and doesn't always collide correctly with objects.
To move your player according to its rotation, use the Rigidbody.AddRelativeForce method.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Game Object won't match rotation of new positions transform 1 Answer

transform position y changes when character rotates 0 Answers

Camera rotation around player while following. 6 Answers

Following another object's position/rotation like parent/child relationship? 4 Answers

How do I get GameObjects to look at me? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges