I am trying to program so when i click the right arrow button my cube looks in specific direction and when i click on my left arrow button it looks in another direction, but when i don’t hold my finger on the button the cube keeps looking the last direction it was set to.
Any help out there?
function Update () {
// Rotate around y-axis
var newRotation = Input.GetAxis("Horizontal") ;
if (newRotation > 0.00001)
{
var directioncarl = 0.0;
transform.Rotate(0, directioncarl, 0);
}
if (newRotation < -0.0000000001)
{
var directioncarlneg = 180;
transform.Rotate(0, directioncarlneg, 0);
}
}
Transform.Rotate adds an extra rotation to the object’s current rotation. If you want to set a particular angle of rotation, you need to set the transform.rotation property directly. I’m not completely clear about what the behaviour of the object is supposed to be, but you probably need to use Quaternion.Euler or Quaternion.LookRotation to set the rotation.
i changed to code to this. Now the cube kind of rotates. I see the rotation value in the inspector change but the z-axe of my object doesn’t flip aroudn the y axe-???
function Update () {
// Rotate around y-axis
var newRotation = Input.GetAxis("Horizontal") ;
if (newRotation > 0.00001)
{
var directioncarl = 0.0;
transform.rotation = Quaternion.Euler (0, directioncarl, 0);
}
if (newRotation < -0.0000000001)
{
var directioncarlneg = 180;
transform.rotation = Quaternion.Euler (0, directioncarlneg, 0);
}