Hello community
I’m making a 2.5D sidescrolling game (2d gameplay so only they cannot move on the Z position), and trying to implement that the character can throw an axe in an arc. The arc part is done by adding gravity to the projectile and a certain speed when the projectiles are instantiated.
However, I’m stuck on aiming with the mouse.
I’ve tried various codes such as the two below:
public float speed = 5f;
private void Update()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward)
}
and
void Update()
{
Vector3 direction = Input.mousePosition - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = rotation;
}
The closest to remotely make it rotate was just to write the following code:
Vector3 direction = Input.mousePosition - transform.position;
But doing this doesn’t take in consideration of the gamescreen, and it seems it only makes it rotate on the rotation.x axis, and what I desire is it to rotate on the rotation.z axis.
However, this apparently makes my projectile not go towards the direction its supposed to.
In case my projectile-instantiation codes are relevant:
void Update()
{
if (timeBtwAttack <= 0)
{
player = GameObject.Find("Player").GetComponent<Player>();
if (Input.GetKeyDown(KeyCode.Mouse1))
{
if(player.facingRight == true)
{
GameObject fireballInstance;
fireballInstance = Instantiate(throwingAxe, spawnPoint.position, spawnPoint.rotation) as GameObject;
fireballInstance.GetComponent<Rigidbody2D>().AddForce(spawnPoint.right * speed);
}
else
{
GameObject fireballInstance;
fireballInstance = Instantiate(throwingAxe, spawnPoint.position, Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
fireballInstance.GetComponent<Rigidbody2D>().AddForce(-spawnPoint.right * speed);
}
timeBtwAttack = startTimeBtwAttack;
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
Hope somebody can guide me in the right way.