'Unity 2d projectile is having a problem with directions
I edited a bit to be more discriptive
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 projectile is still changing the direction after the spawn, depending on the mouse position.
What I wanted VS What it became
This is the video of the footage in it. https://youtu.be/WI9xuILU9Rg
This is the script in the weapon that spawns the projectiles.
if(timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
And this is the script in the projectile.
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);
}
these scripts are in the update function.
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
I solved my problem! it was a problem that accured because I put the vector difference in the update. I made a bool variable that contains the difference and put them in the start function, and it worked! thank you so much for the answers!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jay |
Solution 2 | ximera |
Solution 3 | AliceFromAliceland |