Limit Player Rotation Up and Down - c#

I have the basic movement and rotation working however I can not work out a way to limit the rotation up and down. I want to make it so that you cant look more than 90° up and down.
Ive tried multiple ways such as using if statments and using clamp.
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.Rotate(-rotY, 0, 0);
}
}
With this code you will be able to rotate the camera beyond 90 degrees causing it to be upside down etc

"Camera" is a built-in unity class, I would recommend renaming it to "camera".
Try this to clamp the camera's rotation:
(with your other public floats)
public float minAngle = -90;
public float maxAngle = 90;
(at the end of FixedUpdate)
Vector3 temp = camera.transform.localEulerAngles;
camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(Mathf.DeltaAngle(0, temp.x), minAngle, maxAngle), temp.y, temp.z);
Edit: changed eulerAngles to localEulerAngles
Edit 2: changed the order of the arguements of Mathf.DeltaAngle

I fixed it here. Not to sure on how it works but it works. Credit to video:https://www.youtube.com/watch?v=F5eE1YL1ZJY
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
public float minAngle = -90f;
public float maxAngle = 90f;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY -= Input.GetAxis("Mouse Y") * sensitivity;
rotY = Mathf.Clamp(rotY, minAngle, maxAngle);
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
}
}

Related

Unity - FPS rigid-body controller not aligning to spherical gravity

I'm currently working on a fps game in which you can travel to asteroids and walk on them, currently I have gravity for those asteroids and I have a fps controller that works for all my other movement systems (swimming, floating) but translating it to the gravity of the asteroid is giving me a lot of trouble. I can get the controller to work perfectly on flat surfaces using Unity's gravity but not on the asteroid.
The current fps controller moves in the direction of the camera which means the character floats up when looking up, all my other fps controllers didn't work at all with the planet and this one is the closet I've gotten to getting it to work.
Is there anyway to fix this? I'm quite new to coding so any explanations would help me heaps!
Here's the code I'm using for the controller:
public Transform targetCamera;
public Rigidbody targetRigidbody;
public Transform targetBody;
public float walkSpeed = 9f;
public float runSpeed = 14f;
public float maxSpeed = 20f;
public float jumpPower = 30f;
public float extraGravity = 0;
float bodyRotationX;
float camRotationY;
Vector3 directionIntentX;
Vector3 directionIntentY;
float speed;
public bool grounded;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private const float Sensitivity = 50f;
private const float SensitivityMultiplier = 1f;
private float desiredX;
public float dragMult = 2f;
public float rotationSmoothSpeed = 10f;
void Update()
{
Look();
targetRigidbody.drag = dragMult;
}
private void FixedUpdate()
{
if (targetRigidbody.velocity.magnitude > maxSpeed)
{
targetRigidbody.velocity = targetRigidbody.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
AddForce(Vector3.forward);
if (Input.GetKey(KeyCode.S))
AddForce(Vector3.back);
if (Input.GetKey(KeyCode.A))
AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
AddForce(Vector3.right);
if (Input.GetKey(KeyCode.Space))
AddForce(Vector3.up);
if (Input.GetKey(KeyCode.LeftControl))
AddForce(Vector3.down);
}
private void AddForce(Vector3 direction)
{
float scaledForwardSwimSpeed = Time.deltaTime * forwardSwimSpeed;
targetRigidbody.AddForce(targetCamera.transform.TransformDirection(
direction * thrust) * scaledForwardSwimSpeed,
ForceMode.Acceleration);
}
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
Vector3 rotation = targetCamera.transform.localRotation.eulerAngles;
desiredX = rotation.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
targetCamera.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
}
And here's the first of two bits of code for the asteroid: GravityCtrl
public float gravity = 10f;
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<GravityCtrl>())
{
other.GetComponent<GravityCtrl>().gravity = this.GetComponent<GravityOrbit>();
}
Here's the second: GravityOrbit
public GravityOrbit gravity;
private Rigidbody rb;
public float rotationSpeed = 20f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
if (gravity)
{
Vector3 gravityUp = Vector3.zero;
gravityUp = (transform.position - gravity.transform.position).normalized;
Vector3 localUp = transform.up;
Quaternion targetrotation = Quaternion.FromToRotation(localUp, gravityUp) * transform.rotation;
transform.up = Vector3.Slerp(transform.up, gravityUp, rotationSpeed * Time.deltaTime);
rb.AddForce((-gravityUp * gravity.gravity) * rb.mass);
}

Is there a reason why it isn't sensing my input.getaxis

I even set up public variables to see if was getting any input, and it isn't. I even copied and pasted the names directly from the project settings page.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class vehicle : MonoBehaviour
{
private Vector3 thrusterInput;
public float rotationSpeed = 10f;
public float rotSmoothSpeed = 10f;
private Quaternion smoothedRot = Quaternion.identity;
private Quaternion targetRot;
Body[] bodies;
public Rigidbody rigidbody;
public float thrustStrength = 10f;
// Start is called before the first frame update
void Start()
{
bodies = FindObjectsOfType<Body>();
}
// Update is called once per frame
void Update()
{
foreach(Body body in bodies)
{
float sqrDst = (body.transform.position - transform.position).sqrMagnitude;
Vector3 forceDir = (body.transform.position - transform.position).normalized;
Vector3 acceleration = forceDir * Universe.gravitationalConstant * body.rigid.mass / sqrDst;
rigidbody.AddForce(acceleration, ForceMode.Acceleration);
}
Vector3 thrustDir = transform.TransformVector(thrusterInput);
rigidbody.AddForce(thrustDir * thrustStrength, ForceMode.Acceleration);
rigidbody.MoveRotation(smoothedRot);
}
void HandleMovement()
{
float thrustInputX = Input.GetAxis("Horizontal");
float thrustInputY = Input.GetAxis("Ascent");
float thrustInputZ = Input.GetAxis("Vertical");
thrusterInput = new Vector3(thrustInputX, thrustInputY, thrustInputZ);
float yawInput = Input.GetAxis("Mouse X") * rotationSpeed;
float pitchInput = Input.GetAxis("Mouse Y") * rotationSpeed;
float rollInput = Input.GetAxis("Roll") * rotationSpeed;
Quaternion yaw = Quaternion.AngleAxis(yawInput, transform.up);
Quaternion pitch = Quaternion.AngleAxis(pitchInput, transform.right);
Quaternion roll = Quaternion.AngleAxis(rollInput, transform.forward);
targetRot = yaw * pitch * roll * targetRot;
smoothedRot = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * rotSmoothSpeed);
}
}
Sorry if the indents are all messed up, stackoverflow wasn't letting me use tab for some reason.
Your HandleMovement() is not called from anywhere. You need to put it in Update()

Floating Problem With Rigidbody.Velocity in Unity

Below is the code that I am using to move an object in Unity. The rb.Velocity line makes my object float in in Gameplay mode. If I comment out the line then the object falls just fine.
Could someone explain whats happening here?
public class PlayerController : MonoBehaviour
{
public float forwardVelocity = 0F;
public float maxSpeed = 180;
public float acceleratePerSecond = 8.0F;
public float rotateSpeed = 3.0F;
private float yaw = 0.0f;
private float pitch = 0.0f;
protected Rigidbody rb;
float timeZeroToMax = 2.5F;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
acceleratePerSecond = maxSpeed / timeZeroToMax;
forwardVelocity = 0F;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
{
if (forwardVelocity< maxSpeed)
{
forwardVelocity += acceleratePerSecond * Time.deltaTime;
}
}
forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);
rb.velocity = transform.forward * forwardVelocity;
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
yaw += rotateSpeed * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
The transform.forward is a Vector3(0,0,1), that's the problem.
You are setting your y velocity to 0.

Unity Player Controller - how to make it so it doesn't go upside down

This is my CamMouseLook script and I need it so when the player moves the mouse all the way up it doesnt turn upside down. I want him to be able to look up just not so much up that it turns the view upside down
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMouseLook : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
}
What you can do is to lock the rotation in a specific axis. For example to limit in the Y and X axis so the player can only rotate from [-60,60] degrees you can use:
using System;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 70.0f;
public float clampAngle = 60.0f;
private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update ()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
}
Now you can adapt this script to limit the rotation in the angle and the range you need

Unity fps rotation camera

In my game I have a camera and I want to have an FPS like rotation attached to this camera.
So if I move my cursor to the left, I want my cam to rotate to the left. If I move my cursor up, then the cam should look up, etc.
I currently have it partially working. I can look left and right, up, and down. The problem occurs when I look down and then move my cursor left and right. It then gives me a "Roll" effect.
See this video to see exactly what I mean:
http://www.screencast.com/t/Phedh8H0K13
Obviously when I look down I still want to have a "Yaw" effect instead of a "Roll" effect. Anyone any idea how to do that? This is what I have so far:
// Update is called once per frame
public override void update ()
{
this.camera.transform.rotation *=
Quaternion.AngleAxis( Time.deltaTime * sensitivityRoll * Input.GetAxis("Vertical"), Vector3.forward );
this.camera.transform.rotation *=
Quaternion.AngleAxis( Time.deltaTime * sensitivityYaw * Input.GetAxis("Mouse X"), Vector3.up );
this.camera.transform.rotation *=
Quaternion.AngleAxis( Time.deltaTime * sensitivityPitch * Input.GetAxis("Mouse Y"), Vector3.left );
}
I just found my answer in this topic:
http://forum.unity3d.com/threads/109250-Looking-with-the-Mouse?highlight=person+camera
The code from that topic:
C# Mono code:
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added.
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
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;
void Update ()
{
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);
}
else if (axes == RotationAxes.MouseX)
{
transform.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);
}
}
void Start ()
{
//if(!networkView.isMine)
//enabled = false;
// Make the rigid body not change rotation
//if (rigidbody)
//rigidbody.freezeRotation = true;
}
}
Simple Script, Not Sure it will work with you, might as well give it a try
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class mouse_lookat : 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);
}
}
using UnityEngine;
public class CameraRotator : MonoBehaviour
{
[SerializeField] private Transform _head;
[SerializeField] private float _sensitivity;
[SerializeField] private bool _invertVertical;
private void Update()
{
var deltaMouse = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y");
Vector2 deltaRotation = deltaMouse * _sensitivity;
deltaRotation.y *= _invertVertical ? 1.0f : -1.0f;
float pitchAngle = _head.localEulerAngles.x;
// turns 270 deg into -90, etc
if (pitchAngle > 180)
pitchAngle -= 360;
pitchAngle = Mathf.Clamp(pitchAngle + deltaRotation.y, -90.0f, 90.0f);
transform.Rotate(Vector3.up, deltaRotation.x);
_head.localRotation = Quaternion.Euler(pitchAngle, 0.0f, 0.0f);
}
}
i created 2 scripts for my FPS project that work flawlessly for my movement (currently havnt added jump mechanics) and it seems like youve overcomplicated alot of things that can be quite simple and straight forward.
im also new to coding so please correct me if im wrong heres my code
// this is in the player script which i put on an empty parent object of the camera and player sprite
public class player : MonoBehaviour
{
[Header("player Movement")]
[SerializeField] float moveSpeed = 5f;
[SerializeField] public float mouseSensitivity = 5f;
Rigidbody rigidBody;
// Use this for initialization
void Start()
{
}
void Update()
{
Move();
}
private void Move()
{
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivity);
}
//this is the camera movement script which i put on the camera(will also use this script to add a function to change from 1st person to 3rd person for certain abilities)
{
player player;
// Use this for initialization
void Start()
{
player = FindObjectOfType<player>();
}
// Update is called once per frame
void Update()
{
MoveView();
}
private void MoveView()
{
transform.Rotate(Vector3.left * Input.GetAxis("Mouse Y") * player.mouseSensitivity);
}
}

Categories

Resources