x


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.

more ▼

asked Mar 15 '12 at 08:11 AM

COLLAnitySV gravatar image

COLLAnitySV
36 21 23 25

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.

Mar 15 '12 at 08:28 AM syclamoth

i don't understand... it's keep flying

Mar 15 '12 at 08:56 AM COLLAnitySV
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Mar 15 '12 at 08:43 AM

fafase gravatar image

fafase
10.4k 9 15 40

thanks its really works!!!

Mar 15 '12 at 09:01 AM COLLAnitySV
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1781
x102
x57
x18
x8

asked: Mar 15 '12 at 08:11 AM

Seen: 967 times

Last Updated: Mar 15 '12 at 09:01 AM