I have a platformer game that has a bunch of red and blue cubes. and i need a way/script so that everytime i press say alt, either the red or the blue cubes come into play. so basically on alt they would shift back and forth into line of play. THanks.
1 Answer
1I highly suggest iTween, makes any kind of movement so easy (i also suggest the iTween visual editor, reduces the amount of coding needed):
http://itween.pixelplacement.com
if using iTween, you can use this code to move the platform:
function MovePlatforms()
if(Input.GetButton ("definekey")
{
iTween.MoveAdd(gameObject,Vector3(5,0,0),2)
}
Like i said, its easier with the visual editor.
If you don’t want to use iTween. You can create an animation where 1 cube moves forward and the other back and then use this:
function MovePlatforms()
if(Input.GetButton ("definekey")
{
Animation.play("moveplatforms");
}
//you can use Animation.Rewind("moveplatforms"); or animation["moveplatforms"].speed = -1.0;to play animations backwards
Heres some references to help you:
http://unity3d.com/support/documentation/ScriptReference/Animation.html
http://unity3d.com/support/documentation/ScriptReference/WrapMode.html (ClampForever should work for this)
http://unity3d.com/support/documentation/ScriptReference/AnimationState.html
Good luck! ![]()
made me feel all fuzzy inside ^^. Just another note. Look into switch...case statements, if you had more than 2 cubes, you would probably need to use one of those. I'm not great with them, so cant really help you. Hope your games goes well!
– timskyes, just make one. And yes, it will work with JS. Heres a handy link for iTween: http://itween.pixelplacement.com/gettingstarted.php I do suggest trying the "manual" animation way too, just so you know how to do it in future.
– timsk"is the moving script suppose to be somewhere else?" you haven't told me where it is ;). The best place for the movement script would be on the player controlled character. the line "iTween.MoveAdd" simply calls the MoveAdd function in the iTween script. Please read the getting started page i linked for you, and the iTween documentation.
– timsk"unexpected token: If." if statements are lowercase... "if" not "If". I understand that you may be a beginner, but please, always try to solve a problem yourself first. You will struggle to learn otherwise. The best thing to do is compare it to other bits of code, so if you had a problem with the if statement, compare it to some if statements in unitys standard assets scripts.
– timsk
By "come into play" do you mean the cube then becomes the players controllable character? Are you trying to switch the players controllable character between the red and blue cubes on the press of a key?
– timsk. _ . . . . _ . Like so? "_" is a cube "." is movement. So you press Alt once, the red cube comes forward, press it again and the red goes back and the blue comes forward?
– timskWell there are 2 ways i can think to do this: 1. iTween (by far the easiest) 2. using an animation thats played "Input.OnKeyDown" Do you use iTween? I will do my best to point you in the right direction if not.
– timsk