Hi, I’m working on 2 projects, both involve the rotation of a gameobject.
In the first case, I need to rotate the camera of 45 degrees on the z-axis if the player types right and -45 degrees is he types left.
in the second one is a platform that I need to rotate. 90 degrees if the player types right and -90 on the other case. The codes are practically the same but in the second case, the platform rotates but not in a correct way. Instead of doing 90, 180, 270, 360, It does 90,-180,-90,0…
Please help me cause I tried everything.
the code is this one
public Transform Coso;
public Vector3 angle;
Rigidbody rb;
public int sinistra = 1;
private void Start()
{
sx = false;
rb = GetComponent<Rigidbody>();
}
void Update()
{
rb.velocity = new Vector3(0, 0, 4*Time.deltaTime);
}
void LateUpdate()
{
if (Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x < Screen.width / 2)
{
sinistra--;
Indici();
}
else if (Input.mousePosition.x > Screen.width / 2)
{
sinistra++;
Indici();
}
}
}
void Indici()
{
if (sinistra == -1)
sinistra = 3;
else if (sinistra == -2)
sinistra = 2;
else if (sinistra == -3)
sinistra = 1;
else if (sinistra > 3)
{
if (sinistra == 4)
sinistra = 0;
else if (sinistra == 5)
sinistra = 1;
else if (sinistra == 6)
sinistra = 2;
else if (sinistra == 7)
sinistra = 3;
}
if (sinistra == 0)
Coso.Rotate(0, 0, 0, Space.World);
else if (sinistra == 1)
Coso.Rotate(0, 0, 90, Space.World);
else if (sinistra == 2)
Coso.Rotate(0, 0, 180, Space.World);
else if (sinistra == 3)
Coso.Rotate(0, 0, -90, Space.World);
}
That’s why you ALWAYS want to start a new scene and test your code, if at all possible. That way you know why it is failing. Animation always wins out over scripting movement.
That is the field that YOU would declare that points at the camera.
using UnityEngine;
// Put this in a new default scene, drag the camera transform to transformToRotate, and press PLAY
// left/right arrow will instantaneously tilt the camera
public class CameraTiltingExample : MonoBehaviour
{
public Transform transformToRotate; // you need to drag something into this!
void Update ()
{
float desiredAngle = 0;
if (Input.GetKey( KeyCode.LeftArrow))
{
desiredAngle = 45;
}
if (Input.GetKey( KeyCode.RightArrow))
{
desiredAngle = -45;
}
transformToRotate.rotation = Quaternion.Euler (0, 0, desiredAngle);
}
}
Or for example, if you want it to be smooth:
using UnityEngine;
// Put this in a new default scene, drag the camera transform to transformToRotate, and press PLAY
// left/right arrow will smoothly tilt the camera
public class CameraTiltingSmoothly : MonoBehaviour
{
public Transform transformToRotate; // you need to drag something into this!
float currentAngle;
const float tiltSpeed = 100;
void Update ()
{
float desiredAngle = 0;
if (Input.GetKey( KeyCode.LeftArrow))
{
desiredAngle = 45;
}
if (Input.GetKey( KeyCode.RightArrow))
{
desiredAngle = -45;
}
currentAngle = Mathf.MoveTowards( currentAngle, desiredAngle, tiltSpeed * Time.deltaTime);
transformToRotate.rotation = Quaternion.Euler (0, 0, currentAngle);
}
}