- Home /
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
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).
Your answer
![](https://koobas.hobune.stream/wayback/20220613194707im_/https://answers.unity.com/themes/thub/images/avi.jpg)