Shooting a projectile with Isometric View
So I've been doing some work on an Isometric Game I am developing entirely by myself and after countless hours working around a "Shoot Projectile" system I can't seem to figure it out. I am getting really close to a solution and by that I mean it's kinda working but not 100%. So first I get the direction of my projectile from the player to the mouse and then instantiate the projectile at players location already facing the mouse.
private void Fire()
{
Vector3 direction = GetMouseHitPoint() - transform.position;
Instantiate(projectile, new Vector3(transform.position.x, 2, transform.position.z), Quaternion.LookRotation(direction.normalized));
}
Vector3 GetMouseHitPoint()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 pos;
if (Physics.Raycast(ray, out hit, 1000, 1 << (int)Layer.Walkable))
{
pos = hit.point;
Debug.Log("Mouse position on world: " + pos);
return pos;
}
return Vector3.zero;
}
Then I use this to make the projectile move. Since its already facing the correct direction all I do is make it move forward.
private void MoveProjectile()
{
Vector3 movement = transform.forward * projSpeed * Time.deltaTime;
transform.position += movement;
}
The problem is the projectile doesn't quite move towards the mouse position but instead seems to be off by a bit like so: As you can see the projectile is a little bit off. This only happens when I try to shoot to the sides. If i shoot directly up or down the result is as intended: Any ideas?
I guess, the problem is that you are detecting the click on the ground (y = 0), but the projectile travels at y = 2, resulting in your offset.
I hadn't thought about that. The reason why I set y=2 is that the player transform is at (0,0,0) and I don't want the projectile to "fly low". I'll try to figure something out thanks for the input tho.