Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

coding help needed with unity and c#!

A topic by LE E Dev created Jan 24, 2021 Views: 378 Replies: 8
Viewing posts 1 to 6
(1 edit)

Just making a simple button to make my player rotate when you press a button(on the canvas not the keyboard). So I made this function:

public void Rotate()
{   
   transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 90, transform.rotation.z);
}

and connected it to my button.

but every time I press the button, it doesn't turn -90 dergrees but it rotates to a strange rotation like 0, -88.6758, 0 and if I press it again it doesn't turn! pls help!

Never mind. Found a way to use transform.Rotate. BUT is there a way to smooth the rotation? Thanks.

One of the main reasons that Quaternions are used in games is because you can smoothly interpolate between them in a pretty sensible way. Have a look at https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html for how to Slerp, all you need to do is linearly vary your interpolation value and you'll get  a smooth transition between two rotations.

Unity - Scripting API: Quaternion.Lerp (unity3d.com)
Unity - Scripting API: Quaternion.Slerp (unity3d.com)

both slerp and lerp seems to not work.

Submitted

Instead of transform.rotation.y in the y spot of the euler use transfom.eulerangles.y.

Submitted

A better way is to keep track of your rotation angle and then in the update method, use 

        Vector3 rotatedVector = Quaternion.AngleAxis(currentAngle, Vector3.up);

Deleted post

I just decieded to use animations to rotate. It doesn't seems to work anyways.