- Home /
Help with addforce
Arrow instantiates but doesn't have any force added so it just drops straight down.
var shooter : Transform;
var arrow : Rigidbody;
function Start ()
{
shooter = GameObject.Find("shooter").transform;
}
function Update ()
{
if (Input.GetKeyDown("y"))
{
Instantiate(arrow, shooter.position, shooter.rotation);
arrow.rigidbody.AddForce(Vector3.forward * 1000);
arrow.transform.forward = Vector3.Slerp(arrow.transform.forward, arrow.rigidbody.velocity.normalized, Time.deltaTime);
}
}
Have you tried attaching a rigid body to your arrow and then unticking the gravity check box?
Not sure what you mean by not accessing rigidbody through script
Well you can either add a rigidbody in the inspector to your game object or you can add it through script(accessing here.Sorry for the terrible choice of words).
Yea it already had a rigidbody attached through the inspector
Yea I think what I need to do is make the instantiated object its own variable and it'll work. Also does .rigidbody mean that its attaching a rigidbody to the object? I thought it meant this objects rigidbody.
Answer by getyour411 · Aug 24, 2013 at 09:54 PM
Not sure what shooter and such is in your game, but I copied this into a test project and it fires straight ahead
var shooter : Transform;
var arrow : GameObject;
var thisArrow : GameObject;
function Start ()
{
shooter = GameObject.Find("shooter").transform;
}
function Update ()
{
if (Input.GetKeyDown("y"))
{
thisArrow = Instantiate(arrow, shooter.position, shooter.rotation) ;
thisArrow.rigidbody.AddForce(Vector3.forward * 1000);
thisArrow.transform.forward = Vector3.Slerp(thisArrow.transform.forward, thisArrow.rigidbody.velocity.normalized, Time.deltaTime);
}
}
I had shooter as a child to the FPController object, but that doesn't rotate with the parent so it only fires forward. See if you can adapt that to your setup. Also make note of the remark about using AddForce in Update v FixedUpdate @ http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
thisArrow.Addforce(transform.forward * 1000) this will make it shoot forward in the direction. In your example you would need the .rigidbody before .Addfoce
Your answer
Follow this Question
Related Questions
Rigidbody.AddForce strange with L/R movement? 1 Answer
How to fire a gun using raycasting and still add force 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Player isn't moving (AddForce) (Hook)(c#) 1 Answer
If You add a RelativeForce to an Object like a Sphere, Does Speed ever decrease and stop? 1 Answer