Changing Scale of GameObject on ButtonDown

Hey guys,

Here’s what I am attempting: I wish to change the direction my character faces on the horizontal axis. I figured I could accomplish this by “If” statement with “Input.GetKeyDown” and then change the scale of my object on the x axis to be -1 or 1 depending on the direction I wish my character to face. Obviously, I can’t seem to get it to work.

Here is what I have at the moment.

var speed : float = 6.0;
var gravity : float = 20.0;


private var velocity : Vector3 = Vector3.zero;

function Update() {

    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) 
    {

        velocity = Vector3(Input.GetAxis( "Horizontal" ), 0, Input.GetAxis( "Vertical" ));
        velocity = transform.TransformDirection(velocity);
        velocity *= speed;      
                   
    }
    
    if (Input.GetKeyDown("a")
    	{
    	transform.localScale.x = -1; 
    	}
    
     if (Input.GetKeyDown("d")
   		{
    	transform.localScale.x = 1; 
   		}
   		
   	
    velocity.y -= gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
}

Any help would be greatly appreciated! Taken babysteps here.

To change direction, don’t you want to change rotation, not scale? I don’t even know what a scale of -1 means.

By the way, I have a free sample project on the Asset Store, Fugu TOC which has objects changing scale and direction depending on mouse motion.

My character is made up of a bunch of planes offset in z space, if I rotate the character then you see his back side. But, if I change the scale property in the transform box in the inspector from 1 to -1 it essentially flips the character without changing the rotation.

I’ve taken some screenshots to hopefully demonstrate what I am trying to do.

Scale at 1

Scale at -1

Figured it out!

It’s simple in retrospect. I just needed to add an input for my Input.GetKeyDown to have “a” and “d” as an input. Then used this:

transform.localScale = Vector3(-1,1,1);

to make the scale change.