- Home /
Trying to make a 2d object launch towards the mouse when I left click. can anyone help?
I'm very new to unity and c# and am trying to make the player launch towards the mouse in 2d on a cooldown when I left click. I've tried using raycasts and adding force to the rigidbody, but nothing seems to be working. This seems like it would be a pretty simple code for an experienced person, so if someone could explain it to me that would be much appreciated.
Answer by IvovdMarel · Oct 06, 2020 at 10:29 PM
Hey Falconstrike!
On your post: I recommend in the future to provide a bit more context to your problem. You can share your own code snippets, and explain in more detail what part is not working (e.g. error message, moving in the wrong direction, not moving at all?).
As to your question, you can see this code snippet that has the feature that you are describing. Hopefully you can make sense of it through the comments.
public class ShootToMouse : MonoBehaviour
{
public float forceAmount = 100;
// Update is called once per frame
void Update()
{
//If left mouse button is pressed
if (Input.GetMouseButtonDown(0))
{
//Get the position of this object in screen-coordinates
Vector3 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
//You can calculate the direction from point A to point B using Vector3 dirAtoB = B - A;
Vector3 dirToMouse = Input.mousePosition - posInScreen;
//We normalize the direction (= make length of 1). This is to avoid the object moving with greater force when I click further away
dirToMouse.Normalize();
//Adding the force to the 2D Rigidbody, multiplied by forceAmount, which can be set in the Inspector
GetComponent<Rigidbody2D>().AddForce(dirToMouse * forceAmount);
}
}
}
Answer by enestelli · Oct 06, 2020 at 11:02 PM
Hello @falconstrike209, first of all you need to find the position of the mouse when it's clicked. Input.mousePosition is the way which we use. But this gives us the pixel coordinates of where the mouse was clicked.
We must use Camera.main.ScreenToWorldPoint() function to translate this into world coordinates. Since we want a shot to be in the direction we click, we need to find the direction vector of this shot.
Vector3 lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
This vector shows us in which direction to shoot. Based on this vector, we need to calculate the shooting angle.
float lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg; Mathf.Atan2 is a function that calculates the vector's angle and returns the angle in radians whose tan is y/x. And Mathf.Rad2Deg is a radians-to-degrees conversion constant. It is equal to 360 / (PI * 2). Thus, we find the angle to be shot according to the clicked position. We finally assign this angle on our own rotation to rotate ourselves in the direction we shoot.
transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);
Then we have to instantiate what we will fire.
GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
And we need to access the rigidbody component to give it a speed in the specified direction.
bullet.GetComponent().velocity = transform.up * bulletSpeed;
Finally you can use something like this:
[SerializeField] GameObject bulletPrefab;
[SerializeField] float bulletSpeed;
private Vector3 lookDirection;
private float lookAngle;
void Update()
{
Rotate();
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
private void Rotate()
{
lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);
}
private void Fire()
{
bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
bullet.GetComponent<Rigidbody>().velocity = position.up * bulletSpeed;
}
thank you, however, I wasn't trying to fire a bullet. I was actually launching the player towards the mouse. however, I still appreciate the explanation of these functions.
Wow sadly, I got this question very wrong. I'm happy if I could still teach something.