Search Unity

Flipping character in scale left and right with Joysticks?

Discussion in 'Scripting' started by DarkNeo, May 19, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi guys,

    I have my character moving with the touch controls I am using Unity remote to test I want to get the character to face left when moving left and face right when moving right. I am using the Penelope joystick scripts from mobile assets and also the SideScrollControl.

    Code (csharp):
    1. function FixedUpdate()
    2. {
    3.     var movement = Vector3.zero;
    4.  
    5.         // Apply movement from move joystick
    6.     if ( moveTouchPad.position.x > 0 )
    7.         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
    8.         transform.localScale = new Vector3 (1, 1, 1);
    9.        
    10.         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
    11.         transform.localScale = new Vector3 (-1, 1, 1);
    12.  
    13. }
    This code flips the character scale in -1 when facing left but on play it stays like this, and will not flip to face right. is there an easier code to use to flip the character in the scale when the player is moving the joysticks left and right?

    Thank you


    EDIT: FIXED

    Code (csharp):
    1. //flip character direction
    2.     if(moveTouchPad.position.x > 0){
    3.        transform.localScale = new Vector3(1,transform.localScale.y,transform.localScale.z);
    4.     }
    5.     else if (moveTouchPad.position.x < 0){
    6.        transform.localScale = new Vector3(-1,transform.localScale.y,transform.localScale.z);
    7.     }
    8.  
    9.  
    Just using this under my code and it makes it flip left and right.
     
    Last edited: May 19, 2014