- Home /
What is the best way to make an AI throw something
I have a penguin for a 2.5D sidescroler that I rigged to look like she is throwing a snowball.
How do I make her actually throw a game object...?
Should I animate it on Blender with the penguin then add a rigid body to it.. or is there a script to like launch an object?
Answer by Fattie · Sep 02, 2012 at 07:35 AM
make a new object called snowball. give it the snowball image, and give it a rigidbody. yes, give it gravity.
now, have the snowball sitting offscreen
now, on your penguin you must add a new empty game object, a position marker, called ThrowFromHerePoint
(ie, it would be say 10 cm just in front of his paw, hand, or WTF you call the limb extremities of a 2D penguin !)
ok so you run your animation of the penguin's hand throwing. then you have a little script that does this:
1) position the snowball affair at the point ThrowFromHerePoint
2) and then do this...
function actuallyShoot()
{
_shootDirection = Vector3(1.0, 0.3, 0.0).normalized;
_shootSpeed = 1.234;
theSnowball.rigidbody.velocity = _shootDirection * _shootSpeed;
}
Of course, just tune the direction and speed in there to your liking.
NOTE as a rule, you never set the velocity on objects in game engines. but you're fine doing it in this situation.
3) you're gonna wanna to run that a small moment after you run the hand animation. so your code will look like this:
function throwOneSnowball()
{
theHandThrowingAnimation.Play();
Invoke( "actuallyShoot", 0.0875 );
}
So it will wait a beat before starting the snowball on it's path. (0.0875 seconds ... of course, you'll just tune that time so it looks perfect.)
So the two things you tune to make it look perfect are: the exact position of ThrowFromHerePoint (you typically need it a wee bit forward of the actual muzzle) and the wee delay 0.0875 seconds,
{Welcome to the real-life of real video games - I say casually "pick the best values for the delay and the position". But in fact, very likely you'll have to build a "sled" during production where there is a slider on the screen that lets you adjust those values. Then you can sit there for an hour with your iPad finding the perfect values so it looks realistic. Usually when you build up a title like this, all the "sleds" you need long the way are much more work than the actual game mechanism!}
Be sure to post a vid here when you get it working
Finally you're going to ask about a collider. You can't add a collider to a bullet until you understand the physics Layers system ... there is a huge recent discussion about it here in the ticked answer...
http://answers.unity3d.com/questions/311570/bullets-spawn-behind-the-craft.html
BTW for your scroller, you are using 2DToolkit right?
I would go so far as to say you can't do a 2.5D game without it. I mean you would have to reproduce the 2-4 man-years of work inside 2DToolkit before starting I guess!
Hi and thanks for your reply I will definatly be posting a full beta of the game in the future. I might make a page on facebook ect If you want to be notified when it's up these are both good answers thanks a lot.
No Im not using a toolkit Im doing everything from scratch
Hey People, I Ran into a problem, when i do my script it comes up with an error, it says $$anonymous$$ Identifier "_shootDirection". Could someone please tell me why this is happening, btw i just started with unity.
Answer by CharlesD · Sep 02, 2012 at 07:37 AM
There are a few good ways of doing this... I would recommend making the snowball in blender separate from the penguin and without any motion of being thrown. Next you would import the snowball, attach a kinematic rigidbody to it, and drag it into the hierarchy of your penguin and parent it to whatever animation bone in the penguin that controls his hand (assuming you've animated the penguin to look as if he is throwing something).
Finally you simply write a script to unparent the snowball from the penguin, turn off kinematic mode on the rigidbody and apply a force to the snowball in the direction it should be thrown. All you would need to do then is just execute the script when the penguin's throwing animation reaches the point where the ball should be thrown.
Charles, you make an excellent point explaining how to show the snowball in the style of, you see the snowball sitting in his hand as he walks around etc, and then he throws that one snowball.
I assumed he was doing it "bullet style". ie, the projectile "just appears" from the tip of the fingers as the character throws.
(in other words, you can imagine the penguin throwing ball after ball at the target. there's never actually anything in the penguin's hands, it just appears exactly as a bullet just appears form the muzzle of a cannon as you fire the cannon)
A small point ...
"apply a force to the snowball" you'd probably want to just trivially set the velocity .... if he tries to apply a force it's a all a little harder as he'd have to apply the force over a few frames etc ... it's a situation where it's more meaningful to just "set the velocity" because you are after all just matching the velocity of the hand. However ...
in fact if you're doing a scheme where the parent "lets go" or a "real" ball, you can actually just let go because ... it already has velocity if you set it up right!!! It will fly on it's own. (however in practice, this demands a whole different simulation-approach where the throwing animation has to have realistic speeds and angles etc.)
what you describe is the ultimate sort of "simulation" way to do it. I suggest to Josh it's probably a bit difficult at first, and in any event he'll have to eventually learn how to do an everyday "bullet style", so he should have a go at that here
I couldn't agree more... and at first I had intended on mentioning that depending on the way the snowball was used (background effect, projectile, or simple physics object) would greatly impact the method of implementation. Ultimately when I saw the question your answer had not been added yet, I only saw it when I submitted my own. A simple projectile script is definitely the best solution in this case. If nothing else though my post gives some insight into something fun to experiment with one of these days (after the basics are learned of course).
Your answer
Follow this Question
Related Questions
2.5D Shmup, how to create bullets on "camera plane"? 0 Answers
Raycast and Instantiate 1 Answer
How to add force to an existing object? 1 Answer
Rotate non-kinematic 2D rigidbody on different axis 0 Answers
RigidBody and collision.contacts 0 Answers