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 /
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.

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

How to make Camera position Independent of its Rotation? 1 Answer

How do you make a player move on a remote items local axis? 0 Answers

[solved] Get upward orientation of object and add scale value 1 Answer

setting transform physics2d issue 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