Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by aaronklaser64 · Feb 03, 2012 at 07:14 AM · addforceridigbody

addForce PLEASE HELP!!!

I credit the answerer in my game. this is killing me. i have tried a TON of solutions.

This is all in First person. I have a ball on the ground with rigidbody. When the Fire2(v) is press the ball moves to a player then locks if in place until the Fire1(b) is pressed. This is where im having trouble. i want it to launch the ball with a variable amount force in the direction I'm facing but only about 10 to 15 feet forward in the air. I have try so much im gonna start from scratch based off of your suggestions.

var Ball : Transform; var BallHolder : Transform; var launchAt : Transform;

private var throwForce : float = 10.0; var smooth : float = 5.0;

var ballGrabbed = false;

var shoot : GameObject;

function Update() {

 if(!ballGrabbed)
 {
     if(Input.GetButton("GetCharge"))
     {
         Ball.transform.position =  Vector3.Lerp(Ball.position, BallHolder.position, Time.deltaTime * smooth);     
     }
 }
 
 if(ballGrabbed)
 {
     Ball.transform.position = BallHolder.transform.position;
     
 }    
 
 if(ballGrabbed)
 { 
     Ball.transform.LookAt(launchAt.transform.position);
     if(Input.GetButton("LaunchBall")) 
     {
         ballGrabbed = false;
         

// ---> THIS IS WHERE I NEED HELP TO THROW THE BALL WITH SOME KINDA OF ADDfORCE

     }
 }
 

}

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

1 Reply

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

Answer by aldonaletto · Feb 03, 2012 at 10:03 AM

When you throw, kick or shoot something, it's much easier to set the rigidbody velocity directly. You can calculate an elevation vector (angle degrees above the ground), multiply it by the desired initial speed and assign it to rigidbody.velocity:

 var angle: float = 30.0; // degrees from horizontal plane
 var speed: float = 20.0; // initial velocity (adjust according to the charge)

   ...
   if(Input.GetButton("LaunchBall")) 
   {
       ballGrabbed = false;
       // calculate the elevation vector:
       var dir: Vector3 = transform.forward; // get the forward direction
       dir = Vector3(dir.x, 0.0, dir.z).normalized; // keep it in the horizontal plane
       dir.y = Mathf.Sin(angle * Mathf.Deg2Rad); // set the desired elevation angle
       // apply the velocity to the rigidbody:
       Ball.rigidbody.velocity = speed * dir.normalized;
   }
   ...
Comment
Add comment · Show 4 · 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 aaronklaser64 · Feb 04, 2012 at 02:25 AM 0
Share

Awesome. thanks. but i have one last thing. How do i find the angle based on where im looking, up or down. Thanks again

avatar image aaronklaser64 · Feb 04, 2012 at 02:38 AM 0
Share

How can i changed the angle based on the angle my FPS Camera is facing.

avatar image aldonaletto · Feb 04, 2012 at 09:52 AM 0
Share

The easiest way is to use the camera forward vector directly (no need to do any calculation):

 Ball.rigidbody.velocity = speed * Camera.main.transform.forward;

But if you want to get the camera angle and modify it, you can use this:

 var camAngle = $$anonymous$$athf.Asin(Camera.main.transform.forward.y) * $$anonymous$$athf.Rad2Deg;
 

transform.forward is a normalized vector, thus its y component is numerically equal to the sin of the elevation angle (angle measured from the xz plane).

avatar image game4444 · Nov 02, 2018 at 12:41 PM 0
Share

Hi @aldonaletto its bit late am commenting on this post, I am using your code, thank you so much for helping others in this. Can you guide How can i calculate angle on where mouse is pointing. Like I am throwing ball which is placed on the table and Player has to target bottles and other Objects placed on Shelves or other tables height could be higher then ball. Your code is perfect in throwing but am not able to target properly, using $$anonymous$$ouse Position. Thanks In advance.

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

7 People are following this question.

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

Related Questions

How to get Vector3 of force that WILL BE applied to Rigidbody with AddForce()? 0 Answers

Controlling a moving non-kinematic rigidbody along a path 0 Answers

AddForce on Object at Direction of Click 2 Answers

Unity 2D Physics .AddForce 1 Answer

I can't addforce because of velocity 2 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