- Home /
Fire at Mouse Position, 2d game.
Hi guys!
I'm trying to do a aim system where the bullet will be shot in direction of the mouse position...
I've tried to use Input.mousePosition, but after some researchs , I found that this is relative to pixels not to my transform.position.
I've seen too the Camera.main.ScreenToWorldPoint solution, but that does'nt work how I expected.In my game I instantiate the bullet at the transform.position facing the corresponding side, and after that I apply a vector as the velocity of the bullet.
Does this way of firing right or am I'm going in the wrong direction?
Thank you!
Answer by robertbu · Dec 07, 2013 at 07:05 AM
Here is a simple spawner script that, on a mouse click, shoots the 'prefab' at the mouse position. I had gravity scale set to 0 on the prefab.
#pragma strict
var prefab : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
var go = Instantiate(prefab, transform.position, q);
go.rigidbody2D.AddForce(go.transform.up * 500.0);
}
}
Ohh thank you for the help dude, this script works very well, but can you explain me how do you came to that conclusion? I'm having some difficulties to understand these Quaternions operations.
I've tried this kind of solution and the error that I'm having is that in this script the velocity is proportional to the vector lenght, so when the mouse is far, the velocity is huge, when the mouse is near velocity is low :
void Update ()
{
XvelocityVector =(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) - (transform.position.x);
YvelocityVector = (Camera.main.ScreenToWorldPoint(Input.mousePosition).y) - (transform.position.y);
VelocityVectorCte = YvelocityVector/XvelocityVector;
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
// ... set the animator Shoot trigger parameter and play the audioclip.
anim.SetTrigger("Shoot");
audio.Play();
// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3 (0, 0, 0))) as Rigidbody2D;
Result = new Vector2 (XvelocityVector, YvelocityVector);
bulletInstance.velocity = Result;
}
else
{
// Otherwise instantiate the rocket facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
Result = new Vector2 (XvelocityVector, YvelocityVector);
bulletInstance.velocity = Result;
}
}
}
Answer by coreysnyder · Jul 09, 2014 at 09:55 PM
I'm pretty new to this stuff but I think you want to use the "Normalize" method seen here. What it does it keep the direction the same but sets the magnitude to 1. Then you can take that direction * Missile speed and that will pretty much guarantee that your missile files the same speed in the correct direction no matter the pre-normalized distance
Wow dude, this can be really useful, thank you very much for the help!