Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Panik.Studios · Sep 02, 2012 at 03:34 AM · rigidbody2.5dbulletsthrowing

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Panik.Studios · Sep 12, 2012 at 12:02 AM 0
Share

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.

avatar image Panik.Studios · Sep 12, 2012 at 12:03 AM 0
Share

No Im not using a toolkit Im doing everything from scratch

avatar image Dat Funk · Oct 17, 2013 at 11:25 AM 0
Share

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.

avatar image gheeler · Oct 17, 2013 at 11:42 AM 0
Share

penguins have wings

avatar image Dat Funk · Oct 18, 2013 at 06:45 AM 0
Share

soo, how does that help

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Sep 02, 2012 at 07:53 AM 0
Share

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

avatar image CharlesD · Sep 02, 2012 at 08:19 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges