Question by
KBu · Jan 24, 2016 at 01:00 AM ·
2d gamesprites2d-physics2d sprites2d-gameplay
C# 2D top down shooter game, How do I get the player to shoot towards the mouse cursor?
I'm making a 2D top down shooter type game in c# and I'm stuck with this issue. I'm trying to get my bullet sprites to shoot towards the mouse. I've gotten my player to look towards the mouse but not the bullets.
Here is my shooting script
public class ShootingController : MonoBehaviour
{
float speed;
// Use this for initialization
void Start ()
{
speed = 1f;
}
// Update is called once per frame
void Update ()
{
//Get the initial position
Vector2 position = transform.position;
// objectPos = new Vector3(0, 0, 0);
//Vector3 dir = new Vector3(0, 0, 0);
//objectPos = Camera.main.WorldToScreenPoint(transform.position);
//dir = Input.mousePosition - objectPos;
//Fire the bullet
position = new Vector2(position.x + speed * Time.deltaTime, position.y + speed * Time.deltaTime);
//update the bullet's position
transform.position = position;
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
if(transform.position.y > max.y)
{
Destroy(gameObject);
}
}
}
Here is also some of the player script that instiante it in update function
if(Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject bulletShot = (GameObject)Instantiate(Bullet);
bulletShot.transform.position = bulletPosition.transform.position;
}
Thanks in advance.
Comment