- Home /
How Can I Make a Projectile Move?
I've tried Rigidbodies but I can't seem to get them to work, as they show up for a second (Not literal), and then disappear. When I look at the properties, they get very, very far away. If it helps, I am trying to make a bow fire an "arrow".
#pragma strict
function Start () {
}
function Update () {
if (Input.GetMouseButtonDown(1)) {
var projectile : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
projectile.transform.position = transform.position;
projectile.transform.rotation = transform.rotation;
projectile.transform.localScale = Vector3(0.1, 0.1, 1);
projectile.gameObject.AddComponent(Rigidbody);
projectile.rigidbody.useGravity = false;
projectile.rigidbody.AddForce(Vector3.forward * 1000);
}
}
Answer by Linus · Aug 18, 2013 at 03:53 AM
You should use AddForce not change the drag.
The way you have set it up, force will only be added once, on the frame it was created.
AddForce needs to be constantly added to the object. Make a new script and attach it to the object.
CubeScript.js
//Sample from the documentation
// Adds a force upwards in the global coordinate system
function FixedUpdate () {
rigidbody.AddForce (Vector3.up * 10);
}
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
function Update () {
if (Input.GetMouseButtonDown(1)) {
var projectile : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
projectile.transform.position = transform.position;
projectile.transform.rotation = transform.rotation;
projectile.transform.localScale = Vector3(0.1, 0.1, 1);
projectile.AddComponent(Rigidbody);
projectile.rigidbody.useGravity = false;
projectile.AddComponent(CubeScript);
}
}
Also, make it a habit to leave out the "" when getting or adding components `projectile.gameObject.AddComponent(Rigidbody);
You are learning that is great, but watch/read some tutorials, we are not always gonna be there to hold your hand while your learning.
The forum would be a better place for these questions as well.
Plenty of good ones on youtube and around the web.
(I have no intention of being rude)
I actually tried that and it did the same thing. The drag was an attempt to make it stop moving.
For whatever reason, it still doesn't seem to work. I did edit it so that it'd move forward rather than up, but the Y coordinate still rises the most.
aha, gravity is enabled by default. It needs to be turned off. Updated answer again
Still doesn't want to work the way I really want it to.
Answer by robertbu · Aug 18, 2013 at 05:52 AM
The only problem I see in your script is line 8. While adding force each frame as @Linus suggests is not a bad idea (especially when mimicking something like a missile), it is not necessary. Most projectiles like bullets do it like you do with a single application of force. Line 8 should be:
projectile.rigidbody.Addforce(transform.forward * 1000);
Also line 7 can simplified to:
projectile.AddComponent(Rigidbody);
...since projectile is a game object.