2.5D Sidescroller: Make Character Rotate

I’m trying to make my character rotate when I move in different directions. I’ve tried various methods but none of them are working, and I can’t find any more help online. Here’s my code, in the process movement at the bottom is commented code that I’ve been trying to make work. Thanks for any help.

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public float Gravity = 21f;	 //downward force
	public float TerminalVelocity = 20f;	//max downward speed
	public float JumpSpeed = 6f;
	public float MoveSpeed = 10f;
	public float RunSpeed = 15f;

	public Vector3 MoveVector {get; set;}
	public float VerticalVelocity {get; set;}
	
	private KeyCombo falconPunch= new KeyCombo(new string[] {"down", "right","Fire1"});
	private KeyCombo falconKick= new KeyCombo(new string[] {"down", "right","Fire2"});
	
	//private bool running = false;
	float doubleTapTime = 0.7f;
	private float lastTapTime = 0f;
	private bool doubleTabReady = false;
	private bool walkingForward = false;
	
	private int tr = 90;
	
	public CharacterController CharacterController;
	
void Start () {
		animation.wrapMode = WrapMode.Loop;
		animation["run"].layer = -1;
		animation["walk"].layer = -1;
		animation["idle"].layer = -1;
		
		animation["jump"].layer = 10;
		animation["jump"].wrapMode = WrapMode.Once;
	}
	
// Use this for initialization
void Awake () {
		CharacterController = gameObject.GetComponent("CharacterController") as CharacterController;
	}


// Update is called once per frame
void Update () {
		
		checkMovement();
		HandleActionInput();
		processMovement();
	}

void checkMovement(){
		//move l/r
		var deadZone = 0.1f;
		VerticalVelocity = MoveVector.y;
		MoveVector = Vector3.zero;
		if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone){
			MoveVector += new Vector3(0,0,Input.GetAxis("Horizontal"));
		}
	}

		//jump


void HandleActionInput(){
		if(Input.GetAxisRaw("Vertical") == 1){
		jump();
		}
		
		if (falconPunch.Check())
		{
			// do the falcon punch
			Debug.Log("PUNCH"); 
		}		
		if (falconKick.Check())
		{
			// do the falcon punch
			Debug.Log("KICK"); 
		}
	}

void processMovement(){
		//transform moveVector into world-space relative to character rotation
		MoveVector = transform.TransformDirection(MoveVector);

		//normalize moveVector if magnitude > 1
		if(MoveVector.magnitude > 1){
			MoveVector = Vector3.Normalize(MoveVector);
		}
		
		var currentSpeed = RunSpeed;
		
		// disable double tap after time limit
		if (Time.time > lastTapTime + doubleTapTime){
			doubleTabReady = false;
		}
		
		if (Input.GetAxisRaw("Horizontal") > 0){
			if(!walkingForward){
				walkingForward = true;
				lastTapTime = Time.time;
				if (doubleTabReady){
					currentSpeed = RunSpeed;
					animation.CrossFade ("run");
					Debug.Log ("Running");
				}
			}else{
				doubleTabReady = true;
			}
		}
		
		if (Input.GetAxisRaw("Horizontal") == 0){
			walkingForward = false;
			animation.CrossFade("idle");
		}
		
		if (walkingForward){
			currentSpeed = MoveSpeed;
			animation.CrossFade ("walk");
			//Debug.Log ("Walking");
		}
		
		
//		if (running){
//			currentSpeed = RunSpeed;
//		}else{
//			currentSpeed = MoveSpeed;
//		}
		
		//multiply moveVector by moveSpeed
		MoveVector *= currentSpeed;

		//reapply vertical velocity to moveVector.y
		MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);

		//apply gravity
		applyGravity();
		
		//Vector3 newLookDirection = Vector3.zero;
		//Vector3 axisOfRotation = Vector3.Cross(transform.forward, newLookDirection);
		//float deltaAngle = Vector3.Angle(transform.forward, newLookDirection);
		//Quaternion deltaRotation = Quaternion.AngleAxis(deltaAngle, axisOfRotation);
		//transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * deltaRotation, Time.deltaTime);
		
		
//		if (MoveVector.x > 0){
//			tr = 90;
//		}else{
//			tr = 270;
//		}
//		var euler = transform.eulerAngles.y;
//		euler -= (transform.eulerAngles.y - tr / 5);
//		transform.eulerAngles = new Vector3(0, euler, 0);
		//move character in world-space
		CharacterController.Move(MoveVector * Time.deltaTime);
		
	}

void applyGravity(){
		if(MoveVector.y > -TerminalVelocity){
		MoveVector = new Vector3(MoveVector.x, (MoveVector.y - Gravity * Time.deltaTime), MoveVector.z);
		}
		if(CharacterController.isGrounded && MoveVector.y < -1){
		MoveVector = new Vector3(MoveVector.x, (-1), MoveVector.z);
		}
	}

public void jump(){
		if(CharacterController.isGrounded){
		VerticalVelocity = JumpSpeed;
		animation.Play ("jump");
		}

	}
	
}

Is your character a 3d model object? or a 2d plane with a sprite?. If the second option is correct I used: transform.localScale = new Vector3 (transform.localScale.x *-1, transform.localScale.y, transform.localScale.z) to turn my sprint from right to left and viceversa. Hope this helps

1 Answer

1

Strange no-one has answered your question…

Anyways, this is what I found :slight_smile:

if(MoveVector.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(MoveVector), 1);

Add that before your AddGravity call. It should start turning your character in the correct direction.