- Home /
Catapult press spacebar to launch / throw
hi everyone. i want to ask how to make the catapult throwing an object by pressing space (Not instantiate) ? It's similar with Angry bird game (pull the slingshot and release it, then the object will fly how far according to how far you pull the slingshot). You understand what i mean right ? sorry for my bad english.
any answers appreciated.
Well, the simplest way is to not use hinges or a slingshot at all, and to just use Rigidbody.AddForce in the correct direction. Certainly until your core game mechanics are down, that's what you should be doing.
Answer by fafase · Mar 15, 2012 at 08:43 AM
As I wanted to try for myself as well I came up with a simple form of it. The object in this case is only going up and falling down at a rate that is not the gravity. You still need to perfect it but at least you got a lead. What would you learn if I just give you the answer?
var Start:float;
var End:float;
var force:float = 0.0f;
function Update(){
// when space is pressed down the counter starts
if (Input.GetKeyDown (KeyCode.Space)){
Start = Time.time;
}
// When space is released the counter stops note that GetKeyDown/GetKeyUp does not do anything while keeping pressed
if (Input.GetKeyUp (KeyCode.Space)){
End = Time.time;
force = (End-Start)*20; // here you compare the 2 values I had to multiply by 20 to get it up (that's what she says) but you might have to alter this eqution to get what you want
}
// the result is applied to the object rigidbody, only up in this case here again yo need to work it out
rigidbody.AddForce(Vector3.up*force);
force = force*0.9; // force is decreased in time to get the object falling down
Debug.Log("Start: "+Start+" End: "+End+" Force: "+force);//Just for debug...
}
Hope that helps.