Home > OS >  Unity 2d projectile is having a problem with directions
Unity 2d projectile is having a problem with directions

Time:03-01

So, I was making my projectile move in my 2d game. I realized the projectile was only going to the right, so i figured out I should use a -transform.right in order to make my projectile go to the left when my mouse is on the left.

But it didn't went as I expected. it made my projectile shoot towards left and right, but the x position still changes in the same x for the mouse after the shooting.

This is the video of the footage in it. https://youtu.be/WI9xuILU9Rg

And this is the piece of code I used for the projectile direction.

    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

    if (difference.x > 0)
    {
        transform.Translate(transform.right * projectileSpeed * Time.deltaTime);
    }
    else if (difference.x < 0)
    {
        transform.Translate(-transform.right * projectileSpeed * Time.deltaTime);
    }

can someone help me with this? thank you.

CodePudding user response:

It looks like you have confused what you are looking at for the "direction"

Right now you are moving the bullet to the left/right when the mouse is on the left/right of the bullet. It seems you actually would want to do this for the gun.

The niave solution would simply be to referance the gun's transform and use that to determine the bullets direction.

Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - gun.transform.position;

if (difference.x > 0)
{
    transform.Translate(transform.right * projectileSpeed * Time.deltaTime);
}
else if (difference.x < 0)
{
    transform.Translate(-transform.right * projectileSpeed * Time.deltaTime);
}

However, this would still have a strange effect when you move the mouse (just to the other side of the gun rather than the bullet).

You would be far better encapsulating this in a class and giving the direction only when the bullet is created, as you don't want the direction to change at all after it's been fired.

In Gun.cs

void Fire()
{
    var bullet = GameObject.Instantiate(_bulletPrefab, transform); // ideally pool this
    
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; // gun's transform here

    bullet.GetComponent<Bullet>().Init(difference);
}

Bullet.cs

public void Init(Vector3 direction)
{
    _direction = direction.x > 0 ? transform.right : -transform.right;
}

void Update()
{
    transform.Translate(_direction * projectileSpeed * Time.deltaTime);
}

You might also have an easier time using rigidbody2d.velocity instead of manually translating the object, but this kind of thing should fix your specific problem.

CodePudding user response:

If I understand correctly, your problem is that the projectile keeps following the mouse movement after it has been shot. And if your code is called in Update(), this is caused by the fact that you're getting the mouse position in every frame.

Ideally, what you would want to do is to use the position the mouse had when the user clicked. You can do that by saving the position into a field in your class and use that instead of Input.mousePosition.

class YourClass : MonoBehaviour 
{
    Vector2 mClickPosition;
    
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            mClickPosition = Input.mousePosition;
        }
    
        Vector3 difference = Camera.main.ScreenToWorldPoint(mClickPosition) - transform.position;

        if (difference.x > 0)
        {
            transform.Translate(transform.right * projectileSpeed * Time.deltaTime);
        }
        else if (difference.x < 0)
        {
            transform.Translate(-transform.right * projectileSpeed * Time.deltaTime);
        }
    }
}

Even better would be to save the direction when the user clicks.

  • Related