- Home /
Question by
ZorinAndreyko · Apr 21 at 09:32 AM ·
camera-movement
Why my camera Start to change position???
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public GameObject cam;
public Rigidbody rb;
bool isSprinting = false;
public float sprintingMultiplieer;
public float speed = 0.1f;
public float Xsens = 2;
public float Ysens = 2;
public float Minimumx = -60f;
public float MaximumX = 4.05f;
float x;
float z;
Quaternion camerarot;
Quaternion characterRot;
void Start()//All Start
{//////////////////////
rb = this.GetComponent<Rigidbody>();
camerarot = cam.transform.localRotation;
characterRot = this.transform.localRotation;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
isSprinting = true;
}
else
{
isSprinting = false;
}
}
private void FixedUpdate()//all Controls
{///////////////////////////////////////
float yRot = Input.GetAxis("Mouse X");
float xRot = Input.GetAxis("Mouse Y");
camerarot *= Quaternion.Euler(-xRot * Xsens, 0, 0);
characterRot *= Quaternion.Euler(0, yRot * Ysens, 0);
this.transform.localRotation = characterRot;
cam.transform.localRotation = camerarot;
camerarot = ClampRotationAround_X_Axes(camerarot);
camerarot.z = 0;
x = Input.GetAxis("Horizontal") * speed;
z = Input.GetAxis("Vertical") * speed;
if (isSprinting == true)
{
x *= sprintingMultiplieer;
z *= sprintingMultiplieer;
}
transform.position += cam.transform.forward * z + cam.transform.right * x; //new Vector3(x * speed, 0, z * speed);
}
//private void Update()
//{
// camerarot = ClampRotationAround_X_Axes(camerarot);
//}
Quaternion ClampRotationAround_X_Axes(Quaternion q)//nullQuaternion
{//////////////////////////////////////////////////////////////////
q.x /= q.w;
q.z /= q.w;
q.y /= q.w;
q.w = 1.0f;
float AngelX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
AngelX = Mathf.Clamp(AngelX, Minimumx, MaximumX);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * AngelX);
return q;
}
}
alt text
screenshot-2022-04-19-at-142601.png
(431.6 kB)
screenshot-2022-04-19-at-141440.png
(450.3 kB)
Comment
Your answer
Follow this Question
Related Questions
How to check if a variable has gone up or down 1 Answer
Strange thing happening 1 Answer
Camera view level 2 Answers
OVRPlayercontroller camera is detached from player 0 Answers
Android touches called the Input.GetAxis('Mouse X') 0 Answers