AddForce Isn't working?
I have tried looking this question up but it doesn't come up with an answer to my problem so I have some code that should say on start the object has a force applied to it but it doesn't work? and yes I do have a rigidbody on it
Code:
using UnityEngine;
using System.Collections;
public class HarpoonSharp : MonoBehaviour {
public float power = 10;
public Vector3 shot;
public Rigidbody rb;
void Awake () {
transform.Rotate(Vector3.right, 90);
}
void Start () {
rb = GetComponent<Rigidbody>();
rb.AddForce(shot * power);
}
void Update () {
}
}
I have to rotate it right because of how the model was imported and I have tried disabling that part and it still does not work
Answer by HarshadK · Oct 07, 2015 at 08:20 AM
There is no default value assigned to your shot
variable which means it will be Vector3.zero
by default, which you need to set to some value by either assigning a default value in code or you can even set value from editor since in your Start
method you are using shot * power
which will be zero since your shot
is zero. Also AddForce
will apply force just for one frame in your Start
method, so you have to add enough force to make that object move in that frame by adjusting the value of your power
variable or adding a force of type ForceMode.Impulse
.
By any chance do you know how to make a vector 3 a local variable? like based on where I am looking?