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 Aviratex · Sep 12, 2015 at 10:44 PM · c#rotationmovementplayer

How to make character rotate in direction of movement?

Hi,

I'm trying to make my character rotate in the direction i move in; so if i go forward, the player faces forward, if i go right it faces right, etc. I have checked some other answers tothis problem but they wont work for me. =( Here is my code without any rotation function in it. Also, i want to keep my movement the same as this since it makes the player move in a slippery way, which i like.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed; //determined through inspector
     private float maxSpeed = 15f;
 
     public GameObject deathParticles;
 
     private Vector3 spawnpoint;
 
     private Vector3 userInput;
 
     float delaytime = 1.0f;
 
     void Start() {
 
         spawnpoint = transform.position;
     }
     
     void Update() {
 
         if (delaytime >= 0) {
 
             delaytime -= Time.deltaTime;
             return;
 
         } else {
 
             userInput = new Vector3(Input.GetAxisRaw("LeftnRightMovement"), 0, Input.GetAxisRaw("FornBackMovement"));
 
             if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
             {
 
                 GetComponent<Rigidbody>().AddForce(userInput * moveSpeed);
             }
         }
 }

If anyone could show me how to include the rotation in this script without disrupting the movement it would be great. Also, if possible, please explain how to do it since I am a beginner and I want to understand how it is done and not just copy the code.

Comment
Add comment · Show 4
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 Cherno · Sep 12, 2015 at 11:39 PM 0
Share

Have you tried using Vector3.RotateTowards with your RB's velocity (= movement vector) as the target parameter?

avatar image Aviratex · Sep 13, 2015 at 01:15 PM 0
Share

@Cherno Thanks for the reply. Could you please explain how exactly i can use this? I tried to use this in my code however it did not rotate my cube.

avatar image Cherno Aviratex · Sep 13, 2015 at 01:40 PM 0
Share

Everything you need to know is in the Scripting API. If you are having problems, post your code :)

avatar image Aviratex Cherno · Sep 14, 2015 at 12:51 PM 0
Share

No matter how I try it it wont work. Everything works fine but the cube wont rotate. Is there anything else I need to do before/after the RotateTowards function? I looked at the API but it was no help.

1 Reply

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

Answer by Seyren · Sep 12, 2015 at 11:24 PM

Since i'm not sure what is your case, i'll foward you the easiest way to rotate the character depending on the movement, using a character controller in this case.

Here's the full thing.

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour
 {
 
     public float _gravity = 1.5f;
     public float _yVelocity = 0.0f;
     public float _moveSpeed = 25.0f;
     public float _jumpSpeed = 40.0f;
     public CharacterController _controller;
 
     private bool _onGround;
 
     // Use this for initialization
     void Start ()
     {
         _controller = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         _onGround = _controller.isGrounded; //Gets if the controller is grounded
         Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Gets the direction
         if (direction.sqrMagnitude > 1f) direction = direction.normalized; //This prevents going faster when running diagonically
         Vector3 velocity = direction * _moveSpeed; //Multiplies the movement speed
 
         if (_onGround)
         {
             //JUMPING
             if (Input.GetButtonDown("Jump"))
             {
                 _yVelocity = _jumpSpeed;            //Gets the jump height on the Y Axis
             }
         }
         else
             _yVelocity -= _gravity;
 
         _controller.Move(velocity * Time.deltaTime);
         Vector3 facingrotation = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
         if (facingrotation != Vector3.zero)         //This condition prevents from spamming "Look rotation viewing vector is zero" when not moving.
             transform.forward = facingrotation;
 
     }
 }
 

Keep in mind this requires a character controller and not a rigid body.

If your character is not that complex, this should be good to go.

Comment
Add comment · Show 4 · 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 Aviratex · Sep 15, 2015 at 03:20 PM 0
Share

Love it! With a few changes the rotation part of the script fit perfectly into my own script. Now its nice and simple. The cube rotates properly and does not mess up anything however the rotation is instant and barely noticeable. So if i was moving forward and then i move back the cube instantly rotates 180 degrees. Do you know if there is any way to make the rotation gradual easily (something that a beginner like me can actually understand: not Quaternions and stuff like that) Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Player$$anonymous$$oveandRotation : $$anonymous$$onoBehaviour {
 
     public float moveSpeed; 
     public float maxSpeed;
     
     public GameObject deathParticles;
     public GameObject playerRestPoint; 
 
     private Vector3 spawnpoint;
 
     private Vector3 userInput; 
 
     float delaytime = 1.0f;
 
     void Start() {
 
         moveSpeed = 50;
         maxSpeed = 15f;
 
         spawnpoint = transform.position;
     }
 
     void Update() {
 
 
         if (delaytime >= 0) {
 
             delaytime -= Time.deltaTime;
             return;
 
         } else {
 
             userInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
 
                         if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed) {
 
                             GetComponent<Rigidbody>().AddForce(userInput * moveSpeed);
                         }
 
             Vector3 facingrotation = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
 
             if (facingrotation != Vector3.zero) {
 
                 transform.forward = facingrotation;
             }
         }
     }
 }

However this is the closest I have come to a perfect rotation movement so thanks a lot!

avatar image Seyren · Sep 15, 2015 at 05:10 PM 0
Share

I'm glad it worked :), I was using this in my old character but i ditched it because my character is too complex to just rotate it this way so i went through an animation system, but for something simple it should be good to go.

avatar image Thetrip · Apr 07, 2016 at 02:11 PM 0
Share

hi there!, i used your code on my character(with character controller)the part with the jumping doesn't seem to work i changed get button to key down with space i also put a debug.Log to see if it is indeed pressed which it does and the character just doesn't jump :/ any idea? also in some if's you dont do the { } does this have an impact? all this is so new to me!

avatar image Manyula · Jun 07, 2017 at 05:35 PM 0
Share

This is great, thanks! The only further feature I'd like is to get the smooth transitions between angles, like when you go from North to North-East in your example. How would one go about implementing this for all possible angles?

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

[C#] Rotate player to camera forward look 0 Answers

How can I make the camera not overreach a limit of rotation one axis. 0 Answers

Some problems with handmade basic player controller 0 Answers

moving an object continuously between waypoints 0 Answers

How to move the player only 1 tile per buttonPress? 2 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