Hello everyone, I am having a problem with my script and I would pretty much need help. Basically, I would like my CharacterController to move in the direction of rotation when I press either the Q and E key which in this case is -90° and 90° to the right respectively and it keeps going forward. It works perfectly when I press either of the keys but however, it can only rotate -90° and 90° respectively
What I actually want is that my CC rotates incrementally by -90° or 90° respectively each time I press either of the keys. e.g,
E = 90°,EE=180°,EEE270° and so on… with the reverse been the same for the Q key which is -90° incrementals.
Furthermore, I want it such that assuming the CC starts with a base rotation of 0°, and I press the E key, it rotates 90° to the right which is saved and if I then press the Q key, it should rotate -90 to the new saved rotation . this is how not the case with the script I have written so far for when I press the E key it rotates 90 and if I then press the Q key it immediately rotates -90° which is actually 180° to the rotation of the E key. Below is my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour {
//class variable declarations
private CharacterController controller;
private Vector3 moveVector;
private float speed = 2.0f;
private float jumpSpeed = 8.0f;
private float verticalVelocity = 0.0f;
private float gravity = 20.0f;
// Use this for initialization
void Start () {
controller = GetComponent ();
}
// Update is called once per frame
void Update () {
moveVector = Vector3.zero;
if (controller.isGrounded) {
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetButtonDown ("Jump")) {
verticalVelocity = jumpSpeed;
}
}else {
verticalVelocity -= gravity * Time.deltaTime;
}
if(Input.GetKey(KeyCode.Q)){
transform.rotation = Quaternion.Euler (0, -90, 0);
}
if(Input.GetKey(KeyCode.E)){
transform.rotation = Quaternion.Euler(0,90,0);
}
moveVector.x = Input.GetAxisRaw ("Horizontal") * speed;
moveVector.y = verticalVelocity;
moveVector.z = speed;
moveVector = transform.TransformDirection (moveVector);
controller.Move (moveVector * Time.deltaTime);
}
}