Search code examples
c#unity-game-engine2dprojectile

Unity C#: Projectile speed relative to distance between mouse


I have a problem in Unity where im trying to make instantiate a projectile with a direction and a speed. The direction is dependant on the RotateMouse() function which calculates a direction (for the projectile) in relation to the mouse. After it is normalized, i send it to another a "projectile movement" script which just multiplies it with a speed.

For some reason when i have the mouse close to the character in a certain direction it goes really slow, and the opposite with a long distance... I have also debugged all the values, and every "direction" variable is sub 1 in x,y and z...

Can you spot the problem? The behavior is that it varies in speed despite being normalized, which i'm not understanding.

Script where direction calculation happens:

    public void RotateMouse()
{
    if (Input.GetButton(FindCharacter.FindPlayer(gameObject) + "AimPC"))
    {
        aimingMouse = true;
        sRenderer.enabled = true;
        canShoot = true;

        var mousePos = Input.mousePosition;
        var magePos = Camera.main.WorldToScreenPoint(transform.position);
        difference = mousePos - magePos;

        difference.Normalize(); // after normalize == direction

        rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, rotation_z + spriteRotationOffset));

    }
    else
    {
        aimingMouse = false;
        sRenderer.enabled = false;
        canShoot = false;
    }

}

Another script where i instantiate the projectile and immediately set the direction after:

projectileDir = new Vector3(controller.difference.x, controller.difference.y, controller.difference.z);

    public IEnumerator ShootProjectile(int elementNr)
{
    shooting = true;

    GameObject projectile = Instantiate(projectilePrefabs[elementNr], instantiatePosition, Quaternion.Euler(0, 0, controller.rotation_z + rotationOffsetZ));
    projectile.GetComponent<ProjectileMovement>().Direction(projectileDir);
    yield return new WaitForSeconds(shootDuration);

    shooting = false;

}

And lastly where the direction variable gets used and with calculating the speed:

public class ProjectileMovement : MonoBehaviour {

public float speed;

private Rigidbody2D rb;
private Vector3 direction;
private Collider2D projectileCollider;
private Animator animator;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    projectileCollider = GetComponent<Collider2D>();
    animator = GetComponent<Animator>();
    rb.velocity = speed * direction;
}

public void Direction(Vector3 startingDirection)
{
    this.direction = startingDirection;
}

void FixedUpdate()
{
    rb.velocity = direction * speed;

}
}

And also, if you want more information about the scripts i can provide that, but i think i am showing everything involved with this problem. If something is unclear, then you can comment it.

If the problem is hard to visualize, look at this GIF and observe the fireball velocity relative to the mousepointer:
Gyazo gif link


Solution

  • The issue was that i normalized a Vector3, and it got solved by first normalizing a Vector2 (which gives me a different/correct result) and then put the x and y values into a Vector3 to also make use of the z dimension!