- Home /
vector3.forward doesn't work when used in Rigidbody?
Hello I'm trying to get a ball shooting in the direction that the camera is looking towards. I now got the sphere to spawn facing the direction the camera is facing but when i use bullet.rigidbody.AddForce(Vector3.forward * 1000);
it will simply shoot the sphere in the world's Z axis direction.
complete code
public class Player : MonoBehaviour
{
GameObject bullet;
public float power;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.J))
{
Fire_Bullet();
}
}
void Fire_Bullet()
{
Debug.Log("test");
bullet = (GameObject)Instantiate(Resources.Load("Bullet"), transform.position, transform.rotation);
bullet.rigidbody.AddForce(Vector3.forward * 1000);
}
}
Answer by LukaKotar · Mar 06, 2014 at 11:50 PM
Try bullet.transform.forward
instead.
Thanks This works perfectly! is this working because it has something to do with local/global axes?
that is precisely why. Unity globally defines "forward" as the positive z direction.
Vector3.forward
is the global (world space) Z axis (0,0,1), and transform.forward
is the local Z axis (based on the objects rotation). This is why - as mentioned in the question - it flew along the Z axis in world space. Apologies for late reply.
Answer by Cognitive Dissonance · Mar 07, 2014 at 12:31 AM
I think forward is the (0,0,1) vector which is the Z direction. Im not sure i understand the direction you want to shoot at. If its the x axis i think you use the right vector and if its the y axis the up vector.
Your answer
Follow this Question
Related Questions
C# - Offset In-Air Camera from Current Object Position 1 Answer
Change player movement 0 Answers
CS1502 and CS1503 Error 0 Answers
Launching a Character controller using vectors 0 Answers
How to make a ball stay in the air longer when force is added? 1 Answer