- Home /
2d game facing bullets to mouse
Sorry for reposting question, but i have tried many of solutions and they dont work for me. So i have a 2d plane with a texture(ship) which is shooting bullets. I need bullets to shoot in the direction of mouse. This works, but bullets have strange rotation! Thanks
public class Bullet : MonoBehaviour
{
public float speed = 25f;
void Start()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z);
Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
transform.LookAt(worldPos);
}
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
What do you mean by bullets have strange rotation? As in they move correctly toward the mouse, but the image of the bullet is rotated weird?
There are a couple of different things that might be going wrong here depending on how you constructed your projectile. A screen shot of the wrong rotation would be very helpful as well as a description of how you constructed your bullet. If it is an issue of wrong rotation, then you need to change your bullet so that forward faces positive 'Z'. If it is an issue of flipping, take a look at the optional second parameter to the LookAt() function.
I would try adding Vector3.forward or Vector3.back as the second parameter to LookAt().
Answer by $$anonymous$$ · Jul 20, 2013 at 06:11 PM
Thanks robertbu! This is solution :
{
public float speed = 25f;
void Start()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z);
Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
transform.LookAt(worldPos,Vector3.forward);
}
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
Answer by dshung1997 · Jan 26, 2017 at 05:15 PM
I've met my solution and I immediately wrote this to share with you (I'm a newbie) My main player is a plane and the bullet is kind of thin and long rectangle. I have 2 C# scripts below and they definitely worked (after I changed Update to FixedUpdate in the BulletMotion script)
// This is BulletSpawner script
[SerializeField]
private GameObject bullet;
[SerializeField]
private float fireRate;
[SerializeField]
private GameObject player;
private float lastFire;
// Use this for initialization
void Awake () {
lastFire = 0;
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1") && Time.time > lastFire)
{
lastFire = Time.time + fireRate;
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y));
Vector2 curPos = new Vector2(player.transform.position.x, player.transform.position.y);
Vector2 direction = target - curPos;
direction.Normalize();
GameObject projectile = (GameObject)Instantiate(bullet, curPos, player.transform.rotation);
}
}
.. And below is the BulletMotion script
[SerializeField]
public float speed;
private Rigidbody2D bulletBody;
// Use this for initialization
void Awake () {
bulletBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 temp = transform.position;
Vector3 velocity = new Vector3(0, speed * Time.deltaTime, 0);
temp += ( transform.rotation * velocity);
transform.position = temp;
if (transform.position.y < -6f || transform.position.y > 6f || transform.position.x < -9f || transform.position.x > 9f)
{
Destroy(gameObject);
}
}