Character rotation issue (475069)

I have a script for 2D game, it’s works fine, but I wondered how to make my character look at the movement direction like in 2D tutorial?

var speed = 4.0;
var inAirSpeed = 3.5;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotationSmoothing = 10;
       
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
       
function Update() {
moveDirection.z = 0;

if (!grounded) {
	if (Input.GetKey ("a")){
		moveDirection.x =- inAirSpeed;			
	}
	else {
		moveDirection.x = 0;
		}
	if (Input.GetKey ("d")){
		moveDirection.x = inAirSpeed;
	}
}
    if (grounded){
		if (Input.GetKey ("a")){
			moveDirection.x =- speed;
		}
		else {
			moveDirection.x = 0;
		}
		if (Input.GetKey ("d")){
			moveDirection.x = speed;
		}
		if (Input.GetButtonDown ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}        
	moveDirection = transform.TransformDirection(moveDirection);
	moveDirection.y -= gravity * Time.deltaTime;
	grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime)  CollisionFlags.CollidedBelow) != 0;
}
       
@script RequireComponent(CharacterController)

Add a simple

transform.localEulerAngles = new Vector3 (0,movementDirection,0)
// where movement direction is changed according to whether a or d are pressed, which can be 0 or 180 respectivelly. If ou do that, make sure your character has a 2 sided mesh.

Generally, you want to either flip your chracter via rotation or apply a different texture like in the old days (2 textures, moving left and moving right, or one long texture with all possible frames)

Yes, I need my character to make rotation, and code wont working for me…

I tried to add it in Update:

transform.localEulerAngles = new Vector3 (0,moveDirection,0); but there is no argument for (int, UnityEngine.Vector3, int)’

You have a moveDirection variable. You can use that

transform.rotation = Quaternion.LookRotation( moveDirection );

Sorry Din, my bad, didn’t notice you have a Vector3 moveDirection variable :slight_smile: I meant it as a float, sorry again :smile:

dont work in Update, but when I put it like

	if (Input.GetKey ("a")){
		moveDirection.x =- Speed;
		transform.rotation = Quaternion.LookRotation(Vector3.left );

Character rotates and moves forward, and when Vector3.back - its rotated properly, but moving goes in right not left like it should be…

looks like axis messed up…

Is this a sprite-based 2D as 2D game. Or a 3D as 2D game? If it is a 3D as 2D game3, have you have a chance to look at the 2D platformer tutorial? There is code in there for changing the facing of Lerpz.

2D Game Play Tutorial:

Its a 3D as 2D, and yes, I checked 2D gameplay tutorial, I tried to adapt rotation code part from it, but failed(

Have you tried taking the PlatformerController directly from the tutorial and using that, then modifying it? I remember it working rather well, in context.

Or do your needs differ from the tutorial script?

Either way, can you point me to what you did try?

I’m making a platform sidescroller for mobile, here is what I have for now

      var speed = 4.0;
      var inAirSpeed = 3.5;
      var jumpSpeed = 8.0;
      var gravity = 20.0;
       
      private var moveDirection = Vector3.zero;
      private var grounded : boolean = false;
      
var moveTouchPadR : Joystick;
var moveTouchPadL : Joystick;
var jumpTouchPad : Joystick;
       
      function Update() {
         if (!grounded) {
			if (moveTouchPadL.IsFingerDown()){
				moveDirection.x =- inAirSpeed;
			}
			else {
           		moveDirection.x = 0;
            }
			if (moveTouchPadR.IsFingerDown()){
				moveDirection.x = inAirSpeed;
			}
         }

		  if (grounded){
			if (moveTouchPadL.IsFingerDown()){
				moveDirection.x =- speed;
			}
			else {
				moveDirection.x = 0;
			}
			
			if (moveTouchPadR.IsFingerDown()){
				moveDirection.x = speed;
			}
            if (jumpTouchPad.IsFingerDown())
               moveDirection.y = jumpSpeed;
            }
         

         
         moveDirection = transform.TransformDirection(moveDirection);
       

         moveDirection.y -= gravity * Time.deltaTime;
         

         grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime)  CollisionFlags.CollidedBelow) != 0;
      }
       
      @script RequireComponent(CharacterController)

This script works exactly how I want except facing to the move direction. I tried to use controller from 2D tutorial, but I cant remake it’s inputs for mobile Iniput.touches…

Where is the Joystick class from? Penelope? Or is it your own?

It’s from standard mobile assets.

Have you tried this code from the 2D Platformer Tutorial:

Declare the variable:
(lines 23 - 24 from PlatformerController.js):

	// This controls how fast the graphics of the character "turn around" when the player turns around using the controls.
	var rotationSmoothing = 10.0;

And in function Update ()
(lines 295 - 297 from PlatformerController.js):

	// Set rotation to the move direction	
	if (movement.direction.sqrMagnitude > 0.01)
		transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);

I tried, it was looks like:

if (moveDirection.sqrMagnitude > 0.01)

        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (moveDirection), Time.deltaTime * rotationSmoothing);

but it makes my controller Uncontrolable

Can you give us more detail? In what way is the character uncontrollable? Does Input stop working? Or does this make your character move in a way that you don’t like? I’m unclear…

Here you go

http://dl.dropbox.com/u/18714828/bla/WebPlayer.html

any ideas?