i have a 2D platform game with a plane i wanna add animation to, a up and down wards tilt
This is the script im using to make it fly along the X & Y
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * PlayerSpeed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * PlayerSpeed;
transform.Translate(x, y, 0);
and with my animation script i have this
function Update () { if (Input.GetAxis("Vertical") > 0.2)
animation.CrossFade ("Up");
else
animation.CrossFade ("Idle");
}
i dont know how to add the "Down" with out getting errors.i can only get the up key to work but,my plane then locks it self on position 0,0,0 and i cant move any more the plane will tilt up but thats it? can some one please help me.im a new starter with this java script but im trying to learn and its fustrating me.
What errors are you getting? Do you have animations attached to your plane? or (as it looks from the script above) are you trying to simply move the plane when you press the up or down arrows? These are two completely different approaches.
The former should simply be a case of playing the animation as you've done - just make sure you have those animations on the object the animation script is attached to (or reference the correct child object if they're not). It's case-sensitive, so "Up" and "up" are not the same.
function Update ()
{
if (Input.GetAxis("Vertical") > 0.2)
animation.CrossFade ("up");
else
animation.CrossFade ("idle");
}
For the second approach, there's an example under Input.GetAxis in the scripting docs. Here's a stripped down version that will raise and lower a plane when the up/down arrow keys are pressed. Put this on your plane:
var speed = 10.0;
function Update () {
// Get the vertical axis.
// By default this is mapped to the up/down arrow keys.
// The value is in the range -1 to 1
var translation = Input.GetAxis ("Vertical") * speed;
// monitor the position (look at the bottom left of game window)
Debug.Log(transform.position);
// Make it move in meters per second instead of meters per frame
translation *= Time.deltaTime;
// Move translation along the object's y-axis
transform.Translate (0, translation, 0);
}
Yes my plane has animation clips.my plane moves up and down, left and right 2D platform. the player_control script i have works. but when i add the animation_script to add the tilt's to the movement. it locks my plane modle in position 0,0,0. it make the the tilt when i push up but the plane no longer moves. also
function Update()
{
if (Input.GetKey("w"))
animation.Play("Up");
if (Input.GetKey("s"))
animation.Play("Down");
}
i just tryed this and the play moves up and down with a tilt, but still its, locked on position 0,0,0 in the middle of my screen