How to fix a strage bug in my Abuse-style 2d mouse aiming system?

I’m working on Abuse-style platformer game on Unity but I got stuck with my mouse aiming mechanic. The link illustrates best what I’m trying to achieve, but the basic idea is to move character around in 2d with keyboard and aim+shoot freely (360 degrees) with mouse, regardless of the walking direction.

I managed to make the bullets fly towards the mouse cursor. Unfortunately the speed of the bullets is relative to mouse’s position also. So even though my bullets go in the right direction when I press the button, they go very slowly if my mouse cursor is closer to my player character and vice versa.

Here’s the code:

#pragma strict
var projectile : Rigidbody;

var projectileSpeed  = 130;
var characterOrigin : Vector3;

var targetScreenPos : Vector3;
var targetPosition : Vector3;

var launchVelocity: Vector3;


function Update () {

	targetScreenPos = Input.mousePosition;
	targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos);

//center of the character,  z-axis is 0 becouse this is 2d game
characterOrigin = Vector3(transform.position.x, transform.position.y, 0);

	if (Input.GetMouseButtonDown(0) )
	shootPistol();
}

function shootPistol ()
{

var instantiatedProjectile : Rigidbody = Instantiate(projectile, characterOrigin, transform.rotation );

var targetScreenPos = Input.mousePosition;
var targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos);
var targetDelta = (transform.position - targetPosition) ;

// for some reason I had to  invert this to get the bullets to go correctly.
targetDelta *= -1;

launchVelocity = (targetDelta.normalized * projectileSpeed);

// z-axis is 0 becouse this is 2d game
instantiatedProjectile.velocity = Vector3(launchVelocity.x , launchVelocity.y, 0) ; 

//so the bullet doesn't collide with the player
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

}

So I guess I need to somehow add/subtract (or multiply/divide?) the distance from mouse to player from the bullet speed, but I’m probably wrong since I’ve been trying to do it for three days without results :smile: I’m having troubles of wrapping my head around Vector3’s and I’m also pretty crappy at programming (even though I love doing it). I hope that someone could point me to the right direction!

You’re running into problems because you’re using ScreenToWorldPoint, which gives you a point in 3d which does not lie on the same parallel plane as the one your character moves in. You then normalize the direction from your player, and after strip the z value from it. This is where your problem lies. The closer your pointer is to the player, the direction probably becomes more aimed towards the camera, but as it is normalized, most of it’s length will be in the z axis.
Find it hard to explain in just words, but there’s a pretty easy solution.
Strip the z from targetDelta before normalizing it. Something like this:

var targetDelta = new Vector3(targetPosition.x - transform.position.x, targetPosition.y - transform.position.y, 0) ;
// for some reason I had to  invert this to get the bullets to go correctly.
// targetDelta *= -1; // Just reverse the values like I did above
launchVelocity = (targetDelta.normalized * projectileSpeed);
// z-axis is 0 becouse this is 2d game
instantiatedProjectile.velocity = launchVelocity;

This should do what you want it to, but as the point you get from ScreenToWorldPoint probably won’t be on the same z depth as the player, you will likely experience some amount of inaccuracy. I will leave it to you.

You know what man, your solution worked right off the bat :smile: All I had to do afterwards was to adjust my projectileSpeed. I also understood your explanation, it feels so self evident now but I probably wouldn’t had got it right in a billion years! So thank you very much! Now I can finally move forward with my project, which is great because I already have 5 different weapons, of which none worked :smile: Even though I’m going to miss my slow-motion shotgun… Maybe I should design a new game concept about bullets with different velocities that exploits this bug… or as I like to call it, a feature :slight_smile:

You really made my day!