- Home /
How do I stop my bullets from rotating on the y axis?
Whenever I fire my bullets, they seem to rotate on the y axis, so they aren't visible from a 2D point of view :/ I want them to stay at 0 rotation but i dont understand why they're rotating in the first place. (im a unity newb so good explanation would be greatly appreciated :P)
public class TurretController : MonoBehaviour
{
private Vector3 mouse_pos;
private Vector3 object_pos;
private float angle;
private float bulletSpeed = 500;
public GameObject[] ammo; // Array of enemy prefabs.
// Use this for initialization
void Start()
{
}
void Update()
{
}
void FixedUpdate()
{
// Point the cannon at the mouse.
mouse_pos = Input.mousePosition;
mouse_pos.z = 0.0f;
object_pos = Camera.main.WorldToScreenPoint(transform.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg - 90;
Vector3 rotationVector = new Vector3(0, 0, angle);
transform.rotation = Quaternion.Euler(rotationVector);
// Fire a bullet.
if (Input.GetMouseButtonDown(0))
{
int ammoIndex = 0;
GameObject bullet = (GameObject)Instantiate(ammo[ammoIndex], transform.position, transform.rotation);
bullet.transform.LookAt(mouse_pos);
bullet.rigidbody2D.AddForce(bullet.transform.forward * bulletSpeed);
}
}
}
Here is my code im using (its shooting a bullet from a turret which faces where my mouse is pointing)
Answer by thepenguinmaster · Dec 14, 2014 at 05:41 PM
You may want to look to your prefab. If you have one object nested within another, check that the child rotation is set to zero, and the parent objects are also zero.
Also instead of takign the full rotation form the parent transform, you may consider passing in Vector3.Zero, or new vector3(transform.rtotation.x,0,trnasform.rotation.z)
Do you need your instantiated bullet to rotate at all?
Your answer
Follow this Question
Related Questions
Make sprite turn with rigidbody2D velocity 0 Answers
Weird Output from AngleAxis 0 Answers