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 DubstepDawg · May 05, 2015 at 12:44 PM · movement script

Getting Model to Rotate during movement.

hello all!

Im currently working a small 3d Platformer and i'm having the issue of having my model only move in 4 directions. up down left and right. i cant seem to get the model to rotate based on the inputs i place in on my controller.

Here is the basic code for movement i wrote.

 using UnityEngine;
 using System.Collections;
 
 
 public class UnityChan : MonoBehaviour
 
 {
     private Vector3 movementVector;
     private CharacterController characterController;
     private float movementSpeed = 8;
     private float jumpPower = 15;
     private float gravity = 40;
 
 
     void Start ()
     {
         characterController = GetComponent<CharacterController> ();
     }
 
     void Update()
     {
         movementVector.x = Input.GetAxis("LeftJoystickX") * movementSpeed;
 
         movementVector.z = Input.GetAxis ("LeftJoystickY") * movementSpeed;
 
         if (characterController.isGrounded) 
         {
             movementVector.y = 0;
 
             if(Input.GetButtonDown("A"))
             {
                 movementVector.y = jumpPower;
             }
         }
 
         movementVector.y -= gravity * Time.deltaTime;
 
         characterController.Move (movementVector * Time.deltaTime);
     }
 
 }

any extra help would be highly appreciatged.

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
1

Answer by NathanHold · May 05, 2015 at 02:05 PM

This is some basic vector and quaternion maths.

Basically you want to use your movementVector to get the direction you are moving and face that way. Here is a step by step guide:

 using UnityEngine;
 
 public class UnityChan : MonoBehaviour
 {
     private Vector3 movementVector;
     private CharacterController characterController;
     private float movementSpeed = 8;
     private float jumpPower = 15;
     private float gravity = 40;
     public float lookSpeed = 100.0f;
     
     void Start ()
     {
         characterController = GetComponent<CharacterController> ();
     }
 
     void FaceInput ()
     {
         // First we transform the movement vector, the drawline debug shows what we are
         // doing below.
         Vector3 transformedMovementVector = new Vector3 (movementVector.x, 0, movementVector.z) + transform.position;
 
         // Note: This line is just making sure that we don't look up or down, set this to whatever value is straight
         // if this doesn't work.
         transformedMovementVector.y = transform.position.y;
 
         Debug.DrawLine (this.transform.position, transformedMovementVector, Color.red);
 
         // Get the direction to the transformed vector.
         Vector3 relativePosition = transformedMovementVector - transform.position;
 
         // Make sure we actually need to turn (Otherwise we'll auto correct to facing global fowards.
         if(relativePosition.magnitude > 0)
         {
             // Get the quaternion value of our rotation
             Quaternion rotation = Quaternion.LookRotation (relativePosition);
 
             // Set the rotation 
             // transform.rotation = rotation;
 
             // Use the above if you don't want to have a turn/look speed.
             transform.rotation = Quaternion.RotateTowards (transform.rotation, rotation, lookSpeed * Time.deltaTime);
         }
     }
     
     void Update()
     {
         movementVector.x = Input.GetAxis("Horizontal") * movementSpeed;
         
         movementVector.z = Input.GetAxis ("Vertical") * movementSpeed;
         
         if (characterController.isGrounded) 
         {
             movementVector.y = 0;
             
             if(Input.GetButtonDown("Jump"))
             {
                 movementVector.y = jumpPower;
             }
         }
         
         movementVector.y -= gravity * Time.deltaTime;
         characterController.Move (movementVector * Time.deltaTime);
         FaceInput ();

     }

 }


I recommend getting familiar with Vector and Quaternion maths. See this link for some awesome explanations (It's in 2D but the concepts work through 3D).

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

2 People are following this question.

avatar image avatar image

Related Questions

Player Diagonal Velocity 1 Answer

OnMouseOver() right click cursor - not triggering the code 1 Answer

Want to make GameObject follow another GameObject's path Unity3D 1 Answer

How to get objects with the same script to act independently? 2 Answers

Player Sliding without any Input 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