Need help with turning character 90 degrees

Im a rookie game maker and Im working with pretty simple game. I would like to know how can I make my character turn 90 degrees. I want that when I turn my character it would turn 90 degrees clockwise or turn 90 degrees counterclockwise. Here is my script if you want to look it.

var speed = 3.0;
var rotateSpeed = 3.0;

function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
}

@script RequireComponent(CharacterController)

Just to give you some hints.

If you want to immediately rotate 90 degrees to either side, what you need to do check if the user is trying to turn left or right, to define if the rotation is clock or counter clock-wise (if Input.GetAxis(“Horizontal”) > 0 then the player wants to turn right).

Then, once you detect to which side he wants to turn, just rotate by 90 degrees.

If you are trying to make an FPS game or so, use the CharacterController or the FPSCamera script, both are included in Unity and avoids you all the problem of doing it yourself.

If what you’re looking is to limit the rotation to 90 degrees, just add an if statement to check the local rotation of the player. If it’s above 90 degrees or below -90 degrees, don’t allow any more rotation.

if you want it to turn exactly 90 degrees without actually moving, use transform.position.y - for example when the turn is triggered you would need some code like

transform.position.y = transform.position.y + 90;

that would rotate 90 degrees clockwise i believe, though i cant be sure. hope i was helpful! :smile:

That will move the object 90 units along the y axis. (Theoretically at least - in C# at least I think that code might not have any effect at all, but I can’t remember for sure.)

To rotate an object, you’d need to modify one of the ‘eulerAngles’ or ‘rotation’ member fields.

I found perfect example from youtube :smile:
I want my character turn like the ball in this video

thanks for helping me

that code makes my character disappear when i run the game

when I run my game character immediately turns 90 degrees clockwise but after that it turns like it did before

Script ?

var speed = 3.0;
var rotateSpeed = 3.0;
var yRotation = 5.0;

function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y - axis
yRotation += Input.GetAxis("Horizontal");
    transform.eulerAngles = Vector3(10, yRotation, 0);

// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
}

@script RequireComponent(CharacterController)

Is there something wrong or did i missed something?

Yes, that code is incorrect and won’t do what you’re wanting. (Did you see my earlier post?)

Regarding your script, assuming you’re looking for behavior exactly like that shown in the video, I’d recommend taking a somewhat different approach. (What I gather from watching the video is that the ball can only move in the four cardinal directions; if that’s not correct, then the rest of this post may be irrelevant.)

In order to avoid possible accumulated numerical error, I would not use relative 90-degree rotations for this. Rather, I would just use a single integer (or enum) ‘direction’ variable with four values (representing the four cardinal directions), and for each direction, use a fixed direction vector for movement. (The direction vectors would most likely be (1, 0, 0), (0, 0, 1), etc.)

The player ‘character’ itself could reasonably be represented by the ‘character controller’ component, with height set to 0, resulting in a perfect sphere.

The actual rotation of the ball appears to be a visual effect only. For the visual aspect, I’d create a child entity of the character controller, and then write a script to apply relative rotations based on the ball’s current direction and speed.

Finally, there’s the issue of interpolation; presumably, you’ll want to interpolate both the ball’s ‘yaw’ angle and the camera position when the direction changes. (If the camera is ‘locked’ to the ball’s forward direction, all you need to do is interpolate the yaw angle.)