- Home /
,Trying to flip a sprite to the left from the right side with a mouse
hi im following this tutorial but the sprite disappears when i turn it over 180 degrees, help?
https://www.youtube.com/watch?v=6hp9-mslbzI
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class pivot : MonoBehaviour { private bool m_FacingRight = true; // For determining which way the player is currently facing. public GameObject MyPlayer;
void Update()
{
//Arm Rotation
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
// also check if you are facing the other way
// so you do not call Flip() when you dont need to
if (Mathf.Abs(rotationZ) > 90f && m_FacingRight)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
// Since your player's local scale is fliped
// you'll need to adjust the rotation too
if (!m_FacingRight)
{
rotationZ += 180;
}
// lastly apply the rotation
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
}
,hi, i kinda just started coding and i went online and found a tutorial. i want it to be able to rotate 180 degrees, but if the cursor is to the left, i want the sprite to flip upright. but for some reason, when i put my cursor over 180 degrees, the sprite just disappeared. any solutions?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class pivot : MonoBehaviour { private bool m_FacingRight = true; // For determining which way the player is currently facing. public GameObject MyPlayer;
void Update()
{
//Arm Rotation
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
// also check if you are facing the other way
// so you do not call Flip() when you dont need to
if (Mathf.Abs(rotationZ) > 90f && m_FacingRight)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
// Since your player's local scale is fliped
// you'll need to adjust the rotation too
if (!m_FacingRight)
{
rotationZ += 180;
}
// lastly apply the rotation
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
}