hey, I am trying to make a projectile in my game that has a rigidbody. I don’t know why but it just won’t add the force, however the game lags a lot. here is my code for the projectile.
var Parent : GameObject;
function Start () {
//transform.position = Vector3(281, 4, 288);
Parent = gameObject.Find("Summoner");
transform.position = Parent.transform.position;
}
var body : Rigidbody = gameObject.GetComponent.<Rigidbody>();
function Update () {
body.AddForce(body.transform.forward * 10);
}
If anyone can help, it will be appreciated. Thanks for reading!
Hey for anyone interested, I just solved my problem. I realised that the Rigidbody declaration was in start and using it was in update. here is the working code:
#pragma strict
var Parent : GameObject;
function Start () {
//transform.position = Vector3(281, 4, 288);
Parent = gameObject.Find("Summoner");
transform.position = Parent.transform.position;
}
function Update () {
var body : Rigidbody = gameObject.GetComponent.<Rigidbody>();
body.AddForce(body.transform.forward * 10);
}
you should move that to FixedUpdate, and you should probably cache the rigidbody as well
That projectile will add more & more force over time. If it’s a bullet kind of projectile maybe just use a really high force when the projectile is created & then just let it go. That’s how most projectiles that are not propelled by engines work.