- Home /
Flipping shot point 2d shooter
Im creating a 2D shooter in Unity, and i have the movement script and. the shooting script basically fully complete. But i realized when I'm flipping my character, my shot-points flip erratically, making it so my character is basically shooting 180 degrees from where hes aiming, but flipping back doesnt solve it. Ive been stuck on this for a while, knowing that its probably an extremely simple fix but i cant seem to put my finger on it. Any help is nice.
Heres my aiming code:
private void Update()
{
Vector3 gunPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (gunPos.x < transform.position.x)
{
transform.eulerAngles = new Vector3(transform.rotation.x, 180f, transform.rotation.z);
}
else
{
transform.eulerAngles = new Vector3(transform.rotation.x, 0f, transform.rotation.z);
}
Vector3 mousePos = Input.mousePosition;
Vector3 gunPosition = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - gunPosition.x;
mousePos.y = mousePos.y - gunPosition.y;
float gunAngle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < transform.position.x)
{
transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -gunAngle));
}
else
{
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, gunAngle));
}
And heres my character flip code in my movement script:
if ((velocity.x > 0 && !facingRight) || (velocity.x < 0 && facingRight))
{
flip();
}
void flip()
{
facingRight = !facingRight;
Vector2 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;
}
Answer by Odinwastaken · Oct 15, 2020 at 09:13 AM
Are you saying if the player is shooting left/flipped it still shoots right? if so instead of flipping the players scale just rotate the player instead, example.
//Flipping
private void Flip() {
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
I tried that, but my shot point (where the bullet instantiates in front of the weapon) is unaltered, and if i rotate it with the weapon it shoots backwards. It also messes with my movement script. $$anonymous$$y weapon is ai$$anonymous$$g at the mouse btw.
Your answer
Follow this Question
Related Questions
[2D] How to instantiate laser when player rotates? 1 Answer
Shooting bullets OnMouseDrag, time delay issues 3 Answers
Bouncy/repelling boundaries for a 2D shooter 1 Answer
2D sidescroller shooting 1 Answer
Play two 2D animation together? 2 Answers