I am working on a Unity PlayerLook script and as soon as I enable the vertical rotation code I can look vertically, but my side to side motion is locked, with the camera only jittering. Disabling the vertical rotation code allows my camera to look horizontally.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
CodePudding user response:
If you are making an FPS game I was working on one not too long ago and got a solution like, create a new script and put it in the main camera, then in unity drag and drop your player object to the Player Body component on the Main Camera Script that you just created (Keep note that the script name must be named "MouseDir" for it to work):
I don't know why the Mouse X does not work I had the same problem awhile ago.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDir : MonoBehaviour
{
public Transform playerBody;
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void LateUpdate()
{
transform.position = playerBody.transform.position;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y Input.GetAxis("Mouse X") * sensitivityX;
rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
playerBody.localEulerAngles = new Vector3(0, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
playerBody.Rotate(0, Input.GetAxis("Mouse X ") * sensitivityX, 0);
}
else
{
rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
CodePudding user response:
I got the mouselook and movement to work with this script I found online:
private void Update(){
#region Look Settings - Update
if(enableCameraMovement && !controllerPauseState){
float mouseYInput = 0;
float mouseXInput = 0;
float camFOV = playerCamera.fieldOfView;
if (cameraInputMethod == CameraInputMethod.Traditional || cameraInputMethod == CameraInputMethod.TraditionalWithConstraints){
mouseYInput = mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.X ? Input.GetAxis("Mouse Y") : -Input.GetAxis("Mouse Y");
mouseXInput = mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.Y ? Input.GetAxis("Mouse X") : -Input.GetAxis("Mouse X");
}
else{
mouseXInput= Input.GetAxis("Horizontal") * (mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.Y ? 1 : -1);
} if(targetAngles.y > 180) { targetAngles.y -= 360; followAngles.y -= 360; } else if(targetAngles.y < -180) { targetAngles.y = 360; followAngles.y = 360; }
if(targetAngles.x > 180) { targetAngles.x -= 360; followAngles.x -= 360; } else if(targetAngles.x < -180) { targetAngles.x = 360; followAngles.x = 360; }
targetAngles.y = mouseXInput * (mouseSensitivity - ((baseCamFOV-camFOV)*fOVToMouseSensitivity)/6f);
if (cameraInputMethod == CameraInputMethod.Traditional){ targetAngles.x = mouseYInput * (mouseSensitivity - ((baseCamFOV - camFOV) * fOVToMouseSensitivity) / 6f);}
else {targetAngles.x = 0f;}
targetAngles.x = Mathf.Clamp(targetAngles.x, -0.5f * verticalRotationRange, 0.5f * verticalRotationRange);
followAngles = Vector3.SmoothDamp(followAngles, targetAngles, ref followVelocity, (cameraSmoothing)/100);
playerCamera.transform.localRotation = Quaternion.Euler(-followAngles.x originalRotation.x,0,0);
transform.localRotation = Quaternion.Euler(0, followAngles.y originalRotation.y, 0);
}
#endregion
#region Input Settings - Update
if(canHoldJump ? (canJump && Input.GetButton("Jump")) : (Input.GetButtonDown("Jump") && canJump) ){
jumpInput = true;
}else if(Input.GetButtonUp("Jump")){jumpInput = false;}
if(_crouchModifiers.useCrouch){
if(!_crouchModifiers.toggleCrouch){ isCrouching = _crouchModifiers.crouchOverride || Input.GetKey(_crouchModifiers.crouchKey);}
else if(Input.GetKeyDown(_crouchModifiers.crouchKey)){isCrouching = !isCrouching || _crouchModifiers.crouchOverride;}
}
if(Input.GetButtonDown("Cancel")){ControllerPause();}
#endregion
#region Movement Settings - Update
#endregion
#region Headbobbing Settings - Update
#endregion
}
private void FixedUpdate(){
#region Look Settings - FixedUpdate
#endregion
#region Movement Settings - FixedUpdate
if(useStamina){
isSprinting = Input.GetKey(sprintKey) && !isCrouching && staminaInternal > 0 && (Mathf.Abs(fps_Rigidbody.velocity.x) > 0.01f || Mathf.Abs(fps_Rigidbody.velocity.z) > 0.01f);
if(isSprinting){
staminaInternal -= (staminaDepletionSpeed*2)*Time.deltaTime;
if(drawStaminaMeter){
StaminaMeterBG.color = Vector4.MoveTowards(StaminaMeterBG.color, new Vector4(0,0,0,0.5f),0.15f);
StaminaMeter.color = Vector4.MoveTowards(StaminaMeter.color, new Vector4(1,1,1,1),0.15f);
}
}else if((!Input.GetKey(sprintKey)||Mathf.Abs(fps_Rigidbody.velocity.x)< 0.01f || Mathf.Abs(fps_Rigidbody.velocity.z)< 0.01f || isCrouching)&&staminaInternal<staminaLevel){
staminaInternal = staminaDepletionSpeed*Time.deltaTime;
}
if(drawStaminaMeter){
if(staminaInternal==staminaLevel){ StaminaMeterBG.color = Vector4.MoveTowards(StaminaMeterBG.color, new Vector4(0,0,0,0),0.15f);
StaminaMeter.color = Vector4.MoveTowards(StaminaMeter.color, new Vector4(1,1,1,0),0.15f);}
float x = Mathf.Clamp(Mathf.SmoothDamp(StaminaMeter.transform.localScale.x,(staminaInternal/staminaLevel)*StaminaMeterBG.transform.localScale.x,ref smoothRef,(1)*Time.deltaTime,1),0.001f, StaminaMeterBG.transform.localScale.x);
StaminaMeter.transform.localScale = new Vector3(x,1,1);
}
staminaInternal = Mathf.Clamp(staminaInternal,0,staminaLevel);
} else{isSprinting = Input.GetKey(sprintKey);}
Vector3 MoveDirection = Vector3.zero;
speed = walkByDefault ? isCrouching ? walkSpeedInternal : (isSprinting ? sprintSpeedInternal : walkSpeedInternal) : (isSprinting ? walkSpeedInternal : sprintSpeedInternal);
if(advanced.maxSlopeAngle>0){
if(advanced.isTouchingUpright && advanced.isTouchingWalkable){
MoveDirection = (transform.forward * inputXY.y * speed transform.right * inputXY.x * walkSpeedInternal);
if(!didJump){fps_Rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;}
}
else if(advanced.isTouchingUpright && !advanced.isTouchingWalkable){
fps_Rigidbody.constraints = RigidbodyConstraints.None | RigidbodyConstraints.FreezeRotation;
}
else{
fps_Rigidbody.constraints = RigidbodyConstraints.None | RigidbodyConstraints.FreezeRotation;
MoveDirection = ((transform.forward * inputXY.y * speed transform.right * inputXY.x * walkSpeedInternal) * (fps_Rigidbody.velocity.y>0.01f ? SlopeCheck() : 0.8f));
}
}
else{
MoveDirection = (transform.forward * inputXY.y * speed transform.right * inputXY.x * walkSpeedInternal);
}
#region step logic
RaycastHit WT;
if(advanced.maxStepHeight > 0 && Physics.Raycast(transform.position - new Vector3(0,((capsule.height/2)*transform.localScale.y)-0.01f,0),MoveDirection,out WT,capsule.radius 0.15f,Physics.AllLayers,QueryTriggerInteraction.Ignore) && Vector3.Angle(WT.normal, Vector3.up)>88){
RaycastHit ST;
if(!Physics.Raycast(transform.position - new Vector3(0,((capsule.height/2)*transform.localScale.y)-(advanced.maxStepHeight),0),MoveDirection,out ST,capsule.radius 0.25f,Physics.AllLayers,QueryTriggerInteraction.Ignore)){
advanced.stairMiniHop = true;
transform.position = new Vector3(0,advanced.maxStepHeight*1.2f,0);
}
}
Debug.DrawRay(transform.position, MoveDirection,Color.red,0,false);
#endregion
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
inputXY = new Vector2(horizontalInput, verticalInput);
if(inputXY.magnitude > 1) { inputXY.Normalize(); }
#region Jump
yVelocity = fps_Rigidbody.velocity.y;
if(IsGrounded && jumpInput && jumpPowerInternal > 0 && !didJump){
if(advanced.maxSlopeAngle>0){
if(advanced.isTouchingFlat || advanced.isTouchingWalkable){
didJump=true;
jumpInput=false;
yVelocity = fps_Rigidbody.velocity.y<0.01f? jumpPowerInternal : jumpPowerInternal/3;
advanced.isTouchingWalkable = false;
advanced.isTouchingFlat = false;
advanced.isTouchingUpright = false;
fps_Rigidbody.constraints = RigidbodyConstraints.None | RigidbodyConstraints.FreezeRotation;
}
}else{
didJump=true;
jumpInput=false;
yVelocity = jumpPowerInternal;
}
}
if(advanced.maxSlopeAngle>0){
if(!didJump && advanced.lastKnownSlopeAngle>5 && advanced.isTouchingWalkable){
yVelocity *= SlopeCheck()/4;
}
if(advanced.isTouchingUpright && !advanced.isTouchingWalkable && !didJump){
yVelocity = Physics.gravity.y;
}
}
#endregion
if(playerCanMove && !controllerPauseState){
fps_Rigidbody.velocity = MoveDirection (Vector3.up * yVelocity);
} else{fps_Rigidbody.velocity = Vector3.zero;}
if(inputXY.magnitude > 0 || !IsGrounded) {
capsule.sharedMaterial = advanced.zeroFrictionMaterial;
} else { capsule.sharedMaterial = advanced.highFrictionMaterial; }
fps_Rigidbody.AddForce(Physics.gravity * (advanced.gravityMultiplier - 1));
if(advanced.FOVKickAmount>0){
if(isSprinting && !isCrouching && playerCamera.fieldOfView != (baseCamFOV (advanced.FOVKickAmount*2)-0.01f)){
if(Mathf.Abs(fps_Rigidbody.velocity.x)> 0.5f || Mathf.Abs(fps_Rigidbody.velocity.z)> 0.5f){
playerCamera.fieldOfView = Mathf.SmoothDamp(playerCamera.fieldOfView,baseCamFOV (advanced.FOVKickAmount*2),ref advanced.fovRef,advanced.changeTime);
}
}
else if(playerCamera.fieldOfView != baseCamFOV){ playerCamera.fieldOfView = Mathf.SmoothDamp(playerCamera.fieldOfView,baseCamFOV,ref advanced.fovRef,advanced.changeTime*0.5f);}
}
if(_crouchModifiers.useCrouch) {
if(isCrouching) {
capsule.height = Mathf.MoveTowards(capsule.height, _crouchModifiers.colliderHeight/1.5f, 5*Time.deltaTime);
walkSpeedInternal = walkSpeed*_crouchModifiers.crouchWalkSpeedMultiplier;
jumpPowerInternal = jumpPower* _crouchModifiers.crouchJumpPowerMultiplier;
} else {
capsule.height = Mathf.MoveTowards(capsule.height, _crouchModifiers.colliderHeight, 5*Time.deltaTime);
walkSpeedInternal = walkSpeed;
sprintSpeedInternal = sprintSpeed;
jumpPowerInternal = jumpPower;
}
}
#endregion
#region Headbobbing Settings - FixedUpdate
float yPos = 0;
float xPos = 0;
float zTilt = 0;
float xTilt = 0;
float bobSwayFactor = 0;
float bobFactor = 0;
float strideLangthen = 0;
float flatVel = 0;
//calculate headbob freq
if(useHeadbob == true || enableAudioSFX){
Vector3 vel = (fps_Rigidbody.position - previousPosition) / Time.deltaTime;
Vector3 velChange = vel - previousVelocity;
previousPosition = fps_Rigidbody.position;
previousVelocity = vel;
springVelocity -= velChange.y;
springVelocity -= springPosition * springElastic;
springVelocity *= springDampen;
springPosition = springVelocity * Time.deltaTime;
springPosition = Mathf.Clamp(springPosition, -0.3f, 0.3f);
if(Mathf.Abs(springVelocity) < springVelocityThreshold && Mathf.Abs(springPosition) < springPositionThreshold) { springPosition = 0; springVelocity = 0; }
flatVel = new Vector3(vel.x, 0.0f, vel.z).magnitude;
strideLangthen = 1 (flatVel * ((headbobFrequency*2)/10));
headbobCycle = (flatVel / strideLangthen) * (Time.deltaTime / headbobFrequency);
bobFactor = Mathf.Sin(headbobCycle * Mathf.PI * 2);
bobSwayFactor = Mathf.Sin(Mathf.PI * (2 * headbobCycle 0.5f));
bobFactor = 1 - (bobFactor * 0.5f 1);
bobFactor *= bobFactor;
yPos = 0;
xPos = 0;
zTilt = 0;
if(jumpLandIntensity>0 && !advanced.stairMiniHop){xTilt = -springPosition * (jumpLandIntensity*5.5f);}
else if(!advanced.stairMiniHop){xTilt = -springPosition;}
if(IsGrounded){
if(new Vector3(vel.x, 0.0f, vel.z).magnitude < 0.1f) { headbobFade = Mathf.MoveTowards(headbobFade, 0.0f,0.5f); } else { headbobFade = Mathf.MoveTowards(headbobFade, 1.0f, Time.deltaTime); }
float speedHeightFactor = 1 (flatVel * 0.3f);
xPos = -(headbobSideMovement/10) * headbobFade *bobSwayFactor;
yPos = springPosition * (jumpLandIntensity/10) bobFactor * (headbobHeight/10) * headbobFade * speedHeightFactor;
zTilt = bobSwayFactor * (headbobSwayAngle/10) * headbobFade;
}
}
//apply headbob position
if(useHeadbob == true){
if(fps_Rigidbody.velocity.magnitude >0.1f){
head.localPosition = Vector3.MoveTowards(head.localPosition, snapHeadjointToCapsul ? (new Vector3(originalLocalPosition.x,(capsule.height/2)*head.localScale.y,originalLocalPosition.z) new Vector3(xPos, yPos, 0)) : originalLocalPosition new Vector3(xPos, yPos, 0),0.5f);
}else{
head.localPosition = Vector3.SmoothDamp(head.localPosition, snapHeadjointToCapsul ? (new Vector3(originalLocalPosition.x,(capsule.height/2)*head.localScale.y,originalLocalPosition.z) new Vector3(xPos, yPos, 0)) : originalLocalPosition new Vector3(xPos, yPos, 0),ref miscRefVel, 0.15f);
}
head.localRotation = Quaternion.Euler(xTilt, 0, zTilt);
}
#endregion
#region Dynamic Footsteps
if(enableAudioSFX){
if(fsmode == FSMode.Dynamic)
{
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(transform.position, Vector3.down, out hit)){
if(dynamicFootstep.materialMode == DynamicFootStep.matMode.physicMaterial){
dynamicFootstep.currentClipSet = (dynamicFootstep.woodPhysMat.Any() && dynamicFootstep.woodPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.woodClipSet.Any()) ? // If standing on Wood
dynamicFootstep.woodClipSet : ((dynamicFootstep.grassPhysMat.Any() && dynamicFootstep.grassPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.grassClipSet.Any()) ? // If standing on Grass
dynamicFootstep.grassClipSet : ((dynamicFootstep.metalAndGlassPhysMat.Any() && dynamicFootstep.metalAndGlassPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.metalAndGlassClipSet.Any()) ? // If standing on Metal/Glass
dynamicFootstep.metalAndGlassClipSet : ((dynamicFootstep.rockAndConcretePhysMat.Any() && dynamicFootstep.rockAndConcretePhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.rockAndConcreteClipSet.Any()) ? // If standing on Rock/Concrete
dynamicFootstep.rockAndConcreteClipSet : ((dynamicFootstep.dirtAndGravelPhysMat.Any() && dynamicFootstep.dirtAndGravelPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.dirtAndGravelClipSet.Any()) ? // If standing on Dirt/Gravle
dynamicFootstep.dirtAndGravelClipSet : ((dynamicFootstep.mudPhysMat.Any() && dynamicFootstep.mudPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.mudClipSet.Any())? // If standing on Mud
dynamicFootstep.mudClipSet : ((dynamicFootstep.customPhysMat.Any() && dynamicFootstep.customPhysMat.Contains(hit.collider.sharedMaterial) && dynamicFootstep.customClipSet.Any())? // If standing on the custom material
dynamicFootstep.customClipSet : footStepSounds)))))); // If material is unknown, fall back
}else if (hit.collider.GetComponent<MeshRenderer>()){
dynamicFootstep.currentClipSet = (dynamicFootstep.woodMat.Any() && dynamicFootstep.woodMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.woodClipSet.Any()) ? // If standing on Wood
dynamicFootstep.woodClipSet : ((dynamicFootstep.grassMat.Any() && dynamicFootstep.grassMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.grassClipSet.Any()) ? // If standing on Grass
dynamicFootstep.grassClipSet : ((dynamicFootstep.metalAndGlassMat.Any() && dynamicFootstep.metalAndGlassMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.metalAndGlassClipSet.Any()) ? // If standing on Metal/Glass
dynamicFootstep.metalAndGlassClipSet : ((dynamicFootstep.rockAndConcreteMat.Any() && dynamicFootstep.rockAndConcreteMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.rockAndConcreteClipSet.Any()) ? // If standing on Rock/Concrete
dynamicFootstep.rockAndConcreteClipSet : ((dynamicFootstep.dirtAndGravelMat.Any() && dynamicFootstep.dirtAndGravelMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.dirtAndGravelClipSet.Any()) ? // If standing on Dirt/Gravle
dynamicFootstep.dirtAndGravelClipSet : ((dynamicFootstep.mudMat.Any() && dynamicFootstep.mudMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.mudClipSet.Any())? // If standing on Mud
dynamicFootstep.mudClipSet : ((dynamicFootstep.customMat.Any() && dynamicFootstep.customMat.Contains(hit.collider.GetComponent<MeshRenderer>().sharedMaterial) && dynamicFootstep.customClipSet.Any())? // If standing on the custom material
dynamicFootstep.customClipSet : footStepSounds.Any() ? footStepSounds : null)))))); // If material is unknown, fall back
}
if(IsGrounded)
{
if(!previousGrounded)
{
if(dynamicFootstep.currentClipSet.Any()) { audioSource.PlayOneShot(dynamicFootstep.currentClipSet[Random.Range(0, dynamicFootstep.currentClipSet.Count)],Volume/10); }
nextStepTime = headbobCycle 0.5f;
} else
{
if(headbobCycle > nextStepTime)
{
nextStepTime = headbobCycle 0.5f;
if(dynamicFootstep.currentClipSet.Any()){ audioSource.PlayOneShot(dynamicFootstep.currentClipSet[Random.Range(0, dynamicFootstep.currentClipSet.Count)],Volume/10); }
}
}
previousGrounded = true;
} else
{
if(previousGrounded)
{
if(dynamicFootstep.currentClipSet.Any()){ audioSource.PlayOneShot(dynamicFootstep.currentClipSet[Random.Range(0, dynamicFootstep.currentClipSet.Count)],Volume/10); }
}
previousGrounded = false;
}
} else {
dynamicFootstep.currentClipSet = footStepSounds;
if(IsGrounded)
{
if(!previousGrounded)
{
if(landSound){ audioSource.PlayOneShot(landSound,Volume/10); }
nextStepTime = headbobCycle 0.5f;
} else
{
if(headbobCycle > nextStepTime)
{
nextStepTime = headbobCycle 0.5f;
int n = Random.Range(0, footStepSounds.Count);
if(footStepSounds.Any()){ audioSource.PlayOneShot(footStepSounds[n],Volume/10); }
footStepSounds[n] = footStepSounds[0];
}
}
previousGrounded = true;
} else
{
if(previousGrounded)
{
if(jumpSound){ audioSource.PlayOneShot(jumpSound,Volume/10); }
}
previousGrounded = false;
}
}
} else
{
if(IsGrounded)
{
if(!previousGrounded)
{
if(landSound) { audioSource.PlayOneShot(landSound,Volume/10); }
nextStepTime = headbobCycle 0.5f;
} else
{
if(headbobCycle > nextStepTime)
{
nextStepTime = headbobCycle 0.5f;
int n = Random.Range(0, footStepSounds.Count);
if(footStepSounds.Any() && footStepSounds[n] != null){ audioSource.PlayOneShot(footStepSounds[n],Volume/10);}
}
}
previousGrounded = true;
} else
{
if(previousGrounded)
{
if(jumpSound) { audioSource.PlayOneShot(jumpSound,Volume/10); }
}
previousGrounded = false;
}
}
}
#endregion
#region Reset Checks
IsGrounded = false;
if(advanced.maxSlopeAngle>0){
if(advanced.isTouchingFlat || advanced.isTouchingWalkable || advanced.isTouchingUpright){didJump = false;}
advanced.isTouchingWalkable = false;
advanced.isTouchingUpright = false;
advanced.isTouchingFlat = false;
}
#endregion
}