Easy problem, more difficult solution (for me anyways).
In my current dilemma, I am trying to jump into a teleporter and transport to another location. However, I don’t want to preserve the original camera or player’s rotation, I want it to change to a set value so the player is facing that direction when he goes through the teleporter.
For more information, here is my MouseLook script, and a section from PlayerController (Not Unity prefabs)
MouseLook
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour {
public float sensitivity = 5f;
private float xRotation;
private float yRotation;
[HideInInspector]
public float xRotationCurrent;
[HideInInspector]
public float yRotationCurrent;
private float xRotationVelocity;
private float yRotationVelocity;
public float lookSmoothDamp = 0.1f;
void Awake() {
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
yRotation += (Input.GetAxis("Mouse X") * sensitivity);
xRotation += (Input.GetAxis("Mouse Y") * -sensitivity);
xRotation = Mathf.Clamp(xRotation, -90, 90);
xRotationCurrent = Mathf.SmoothDamp(xRotationCurrent, xRotation, ref xRotationVelocity, lookSmoothDamp);
yRotationCurrent = Mathf.SmoothDamp(yRotationCurrent, yRotation, ref yRotationVelocity, lookSmoothDamp);
transform.rotation = Quaternion.Euler(xRotationCurrent, yRotationCurrent, 0);
if (Input.GetKeyDown(KeyCode.Escape)) {
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetMouseButtonDown(0)) {
Cursor.lockState = CursorLockMode.Locked;
}
}
}
PlayerController
void Walk() {
transform.rotation = Quaternion.Euler(0, camera.GetComponent<MouseLook>().yRotationCurrent, 0);
transform.Translate(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
StartCoroutine(FootstepController(0.55f));
}
void Run() {
transform.rotation = Quaternion.Euler(0, camera.GetComponent<MouseLook>().yRotationCurrent, 0);
transform.Translate(Input.GetAxis("Horizontal") * walkAcceleration * sprintMultiplier, 0, Input.GetAxis("Vertical") * walkAcceleration * sprintMultiplier);
StartCoroutine(FootstepController(0.3f));
}
The problem is: when I enter the teleporter, my rotation is presumably changed, but it immediately reverts back to the original because Input.GetAxis is controlling the rotation value. Since I can not change this value to 0 via script (without adding custom scripts for Unity), does anyone see a fix? I am open to try anything. Let me know if you see something. Thank you.