- Home /
Shoot bullet in direction which player looks
Hi there
I just started again with Unity (did some stuff a few years back) and I have a problem now which I can't solve.
I want to shoot bullets in the direction the player is looking.
I have following setup: Player --Camera --gunBarrel
I have a player script attached to the Player which instantiates the bullets.
playerScript.cs
public class PlayerScript : MonoBehaviour
{
public float MovementSpeed = 1;
public GameObject bullet;
private GameObject _bullet;
public Transform gunBarrel;
void Start ()
{
}
void Update () {
if (Input.GetButtonDown("Fire"))
{
_bullet = Instantiate(bullet, transform.position, transform.rotation);
}
}
}
In the inspector, the player rotates as it should. But the instantiated bullets have a rotation of (3.8,0,0). If I Debug.Log(transform.rotation) I always get 0,0,0 no matter how the player is rotated. Can somebody explain me, what I am doing wrong?
Thanks, mitti2000
Do you actually rotate the object with the PlayerScript attached? Or do you rotate some child object with the visuals attached?
I'm using the Smeeoth$$anonymous$$ouseLook Script (http://wiki.unity3d.com/index.php/Smooth$$anonymous$$ouseLook) Which is attached to the player. The player script is also atteched to the player. In the inpector I see the rotation changes on the player. Does that answer your question?
Hey,
i just recreated your setup and for me everything works. You could try to remake your setup from scratch just to be sure that there is no mistake in it.
I took a cube, attached the smoovemouseLookScript with default values and gave it the camera as a child. Then i attached another script to the Cube that only does a debug.Log(transform.rotation.eulerAngles) in Update. For me this works and provides the correct Angles.
Your answer
Follow this Question
Related Questions
Bullet Rotation faces wrong direction 2 Answers
Problem with Shooting Accuracy 0 Answers
Need help with Transform.LookAt 1 Answer
shoot bullet on a moving gameobject 0 Answers