Question by
ScottySR · Feb 18, 2019 at 12:05 PM ·
shootinganglemouseposition
Shoot towards mouse with offset
In the game I'm making you shoot towards your mouse. When you gain levels you get a 3-way or 5-way shot. To do this I need to be able to add a certain offset to the angle the projectile is shot. All code I've looked up so far either doesn't work or only rotates the sprite, but still shoots the same direction. What is the easiest way to do this? On another note, can Unity even handle movement direction in degrees? To me it seems like you have to do math to break things to X and Y components manually.
Anyway here is my current code:
public class shoot : MonoBehaviour {
public GameObject projectile;
public GameObject point;
stats stats;
Vector2 target;
Quaternion rotation;
float angle;
int cooldown = 0;
void Start()
{
stats = GetComponent<stats>();
}
void Update ()
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg;
rotation = Quaternion.AngleAxis((Mathf.Round(angle / 12.5f) * 12.5f) - 90, Vector3.forward);
if(Input.GetButton("Fire1") && cooldown <= 0)
{
Instantiate(projectile, point.transform.position, rotation);
if(stats.level > 1)
{
//3-way shot on level 2
//Instantiate 2 more projectiles
}
if (stats.level > 2)
{
//5-way shot on level 3 when accounting 2 projectiles added previously
//Instantiate 2 more projectiles
}
cooldown = 5;
}
if(cooldown > 0)
{
cooldown--;
}
}
}
Comment