- Home /
Rotate gameObject if cursor is near screen edges
Hi, My game is FPS and I'm trying to rotate a player's gameobject on horizontal and(or) vertical axes (like in "Operation Flashpoint" game). Also, I need to restrict player's rotation. Thing is when I use transform.localEulerAngles = new Vector3(x,y,z);
player rotates immidiately, not smooth but rotation restriction is working. But if I use transform.RotateAround(position, axis, angle);
player rotates smoothly(perfect!) but no restriction is on. Could you please review my code and tell me what is wrong? Thank you!
protected void processMove(Vector3 delta)
{
crosshairPosition = new Rect(delta.x - crosshairTexture.width/2, Screen.height - (delta.y + crosshairTexture.height/2), crosshairTexture.width, crosshairTexture.height);
var crosshairPositionVector = new Vector3(crosshairPosition.x, crosshairPosition.y);
cameraX += delta.y * -1 * Time.deltaTime * 0.5f;
cameraY += delta.x * Time.deltaTime * 0.5f;
Debug.Log(cameraY + " is cameraY");
cameraX = Mathf.Clamp(cameraX, -20, 20);
cameraY = Mathf.Clamp(cameraY, -20, 20);
if(delta.x >= 630f)
{
//Debug.Log("Right SIDE");
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, cameraY % 180, 0);
//transform.RotateAround(transform.position, transform.up, cameraY % 180);
}
else if (delta.x <= 90f)
{
//Debug.Log("Left SIDE");
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, -cameraY % 180, 0);
}
else if (delta.y >= 450f)
{
//Debug.Log("Upper SIDE");
transform.localEulerAngles = new Vector3(cameraX % 180, transform.localEulerAngles.y, 0);
}
else if (delta.y <= 65f)
{
//Debug.Log("Lower SIDE");
transform.localEulerAngles = new Vector3(-cameraX % 180, transform.localEulerAngles.y, 0);
}
else
{
//Debug.Log("NOTHING");
}
}
Your answer
Follow this Question
Related Questions
Walking animation has wrong rotation 0 Answers
Rotating camera while wallrunning 1 Answer
Trying to rotate a 2D image over time to a specific angle, but it's rotating its Y value every time 1 Answer
Rotate world space UI element on Z axis towards gameobject. 1 Answer
How to get smooth object rotation?? 0 Answers