Rotating character 180 degrees

I`m working on a game like Mario , but I use Lerpz instead of Mario :smile:

I want Lerpz to face the right X axis when I press right and when I press left it rotates 180 to face the left side

can anyone help me with this please?

The simplest way would be, to oriented right as 0 along either the x or z plane (I’m going to choose x) and then doing:

float moveX = Input.GetAxis("Horizontal");
if (moveX==1  transform.eulerAngles.x==180) {
transform.rotation(-180,0,0);
}
else if (moveX==-1  transform.eulerAngles.x==0) {
transform.rotation(180,0,0)
}

That’s not all the code, but what that does is asks what key we’re hitting and then says if we hit left and we’re facing right, rotate, if we’re facing left, do nothing.

Thanks Slev, I will try it and I`ll tell you the result :smile:

Don’t put the words “and” in, I went to Python for a second there. I corrected the code now.

It didn`t work for me buddy :frowning:

it only turns him one time not everytime i click right or left

Does Mario rotate? If not, just set the x scale factor to negative. If you do need to rotate, I would look at either Euler Angle and or iTween.

Here is the code so you can check it well and tell me where is the bug:

function Update () {
	
	var controller : CharacterController = GetComponent(CharacterController);
	
	
	if(controller.isGrounded){
	
		moveDirection.x = 0;
	
		if(Input.GetKey("right") transform.eulerAngles.x == 180){
				transform.rotation.y = -180;
				moveDirection.x = 0.5;
				controller.Move(moveDirection * Time.deltaTime * speed );
		}
	
		if(Input.GetKeyDown("space")){
				moveDirection.y = jumpSpeed;
		
		}
		
		if(Input.GetKey("left")   transform.eulerAngles.x == 0){
			transform.rotation.y = 180;
			moveDirection.x = -0.5;
			controller.Move(moveDirection * Time.deltaTime * speed );

		}
			
	}
	
	if(Input.GetKey("right")){
		moveDirection.x = 2;
	}

	moveDirection.y -= gravity*Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
	
	

}

I am confused at Slev’s original response. In one respect, he checks the euler angle and tests it against 180, in the other he tests it against 0. But, in the rotational set, he sets it to -180. Isn’t -180 and 180 the same rotation? (since they are 360 apart)

Below is that code corrected.

    float moveX = Input.GetAxis("Horizontal");
    if (moveX==1  transform.eulerAngles.x==180) {
    transform.rotation(0,0,0);
    }
    else if (moveX==-1  transform.eulerAngles.x==0) {
    transform.rotation(180,0,0)
    }

Thanks for the correcting bigmisterb… but I`m still facing the same problem :frowning:

bigmisterb I did the -180 because then you don’t rotate 360 all the way, I mean it works either way, I just wanted it to always use the same “path” to get between A and B.

okies, after looking over this, and going over the basic CharacterController movement, I peiced together this:

// of course, you have to debug it, I didnt test it, but the core should be there

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (CharacterController))]


public class example : MonoBehaviour {
	float speed = 8;
	float jumpSpeed = 8;

	float facingSpeed = 3;

	private float direction = 0;
	private float facing = 0;
	private Vector3 move = Vector3.zero;
	private bool isGrounded = false;
	private CharacterController controller;

	void Start(){
		controller = (CharacterController)gameObject.GetComponent<CharacterController>();
	}

	void Update(){
		Vector3 mv = new Vector3(Input.Axis("Horizontal"),0,0);
		
		if(mv.x < 0  direction == 0  ){direction = 180;}
		if(mv.x > 0  direction == 180){direction = 0;  }
		
		facing = Mathf.MoveTowards(facing, direction, Time.deltaTime * facingSpeed);
		transform.eulerAngles = new Vector3(0,facing,0);
		
		if(controller.isGrounded){
			move.y = Physics.Gravity.y * 2 * Time.deltaTime;
			mv *= speed * Time.deltaTime;
			if (Input.GetButton("Jump")){
				move.y = jumpSpeed;
			}
		} else {
			move.y += Physics.Gravity.y * 2 * Time.deltaTime;
			mv *= speed * Time.deltaTime * 0.25f;
		}
		
		
		move += mv;
		move.x = Mathf.Clamp(move.x, -speed, speed);
		move.y = Mathf.Clamp(move.y, -Physics.Gravity.y * 2, jumpSpeed);
		
		controller.Move(move * Time.deltaTime);
		move = controller.velocity;
	}
}

What I did is to store the current and wanted rotations. Then simply change the wanted to be whatever I want it to be, then every frame it would rotate it a little as needed.

I also added in the CharacterController methods of movement, because that would help more than trying to build your own at this point.

Thanks Pal, I am checking it now and I`ll tell you what I get here?

I personally nest the character model as a gameobject inside the gameobject which has the controller.

This make life really simple so when changing direction I multiply the direction vector of the parent by a polarity of 1 or -1 and rotate the inner model between 0 and 180. So the models rotation does not affect the movement direction at all

same problem here… i try the last script but dont work , “unxspected symbol direction” :frowning: plz help

void Update()
{
    float horizontal = Input.GetAxis("Horizontal");
    if (horizontal > 0 && !facingRight)
        Flip();
    else if (horizontal < 0 && facingRight)
        Flip();
}
void Flip()
{
    Vector3 scale = transform.LocalScale;
    scale.x *= -1;
    transform.LocalScale = scale;
}

I think this is a good way to rotate as ‘Character animation’ will rotate too, we store a local vector3 of scale to manipulate it as we can’t manipulate LocalScale directly.
But as I have an animation that changes scale for the character I can’t use this method what i used for the rotation was transform.Rotate(Vector3.up, 180); It worked but i don’t know this is a good way or not