Question by
SINNEDSTAR1 · Dec 28, 2021 at 10:43 PM ·
transform.rotation
Help me figure out how to fix character rotation
Hello ! I cant figure out how to lock the player y movement rotation. Im making a temple run kinda game, if i press D , character rotate to 90f, but that's the maximum i want him to rotate. If i press D again when he is in position y= 90f , i want him to do nothing, same for A which rotate the character to -90. Any ideas on how can i do it ?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playermovement : MonoBehaviour { private bool turnLeft, turnRight; public float speed = 7.0f; public float maxYrotate = 90f; public float minYrotate = -90f;
private CharacterController myCharacterController;
// Start is called before the first frame update
void Start()
{
myCharacterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
turnLeft = Input.GetKeyDown(KeyCode.A);
turnRight = Input.GetKeyDown(KeyCode.D);
if (turnLeft)
transform.Rotate(new Vector3(0f, minYrotate, 0f));
else if (turnRight)
transform.Rotate(new Vector3(0f, maxYrotate, 0f));
Input.GetAxis()
myCharacterController.SimpleMove(new Vector3(0f, 0f, 0f));
myCharacterController.Move(transform.forward * speed * Time.deltaTime);
}
Comment
Your answer
