- Home /
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
}
}
}
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;
}
...
Awesome. thanks. but i have one last thing. How do i find the angle based on where im looking, up or down. Thanks again
How can i changed the angle based on the angle my FPS Camera is facing.
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).
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.