- Home /
Question by
Doom-Holme · Jun 12, 2020 at 09:02 AM ·
rotationscripting problem2d gameproblem during runtimeflipping
2D Sprite constantly flipping
I posted a question yesterday and somehow fixed my issue but a new one came up. Basicly my sprite is constantly flipping at a certain mouse point. I don't really know how to resolve this. Here's the code, and down blow a small clip of the issue.
void Update()
{
move.x = Input.GetAxisRaw("Horizontal");
move.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Speed", move.sqrMagnitude);
// using mousePosition and player's transform (on orthographic camera view)
var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
if (delta.x >= 0 && !facingRight)
{ // mouse is on right side of player
transform.localRotation = Quaternion.Euler(0, 0, 0); // or activate look right some other way
facingRight = true;
}
else if (delta.x < 0 && facingRight)
{ // mouse is on left side
transform.localRotation = Quaternion.Euler(0, 180, 0); // activate looking left
facingRight = false;
}
}
Thats the one from the player object and here's the code from the gun pivot.
public GameObject myPlayer;
private void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
I would really appreaciate some help. Thanks in advance.
kirby-holding-a-gun.gif
(374.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Get a single number for rotation (2d game) when using Quaternion.FromToRotation 1 Answer
Super Mario Bros 2D very lagging 1 Answer
Flip Player, Not the Player's Child. 3 Answers
Flip the player with arm rotation 2 Answers
Enemy Patrol Wait Time 2 Answers