- Home /
Question by
developerguy91 · May 22, 2016 at 03:38 AM ·
rotation1st personridgidbody
Custom character controller randomly rotates after colliding with an object
My character controller after colliding with an any object adds continuous rotation on the y.
I have no idea whats happening...
Motor Script:
using UnityEngine;
using System.Collections;
using System;
[RequireComponent(typeof(Rigidbody))]
public class FirstPerson3DMotor : MonoBehaviour {
private Rigidbody rb;
private Vector3 _velocity = Vector3.zero;
private Vector3 _rotation = Vector3.zero;
private Quaternion targetRotation;
private float camRotation;
private float currentCameraRotationX = 0f;
private float rotationLimit = 80f;
private Camera cam;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
cam = GetComponent<FirstPerson3DController>().playerCamera;
}
// Fixed Update is called every time a physics event (iteration) occurs.
void FixedUpdate ()
{
ApplyMovement();
ApplyRotation();
}
void ApplyRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler(_rotation));
if (cam != null)
{
currentCameraRotationX -= camRotation;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -rotationLimit, rotationLimit);
cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
}
}
void ApplyMovement()
{
if (_velocity != Vector3.zero)
{
rb.MovePosition(rb.position + _velocity * Time.fixedDeltaTime);
}
}
public void Move(Vector3 velocity)
{
_velocity = velocity;
}
public void Rotate(Vector3 rotation)
{
_rotation = rotation;
}
public void RotateCamera(float cameraRotation)
{
camRotation = cameraRotation;
}
public void Jump(float jumpHeight)
{
if(Input.GetKeyDown(KeyCode.Space))
{
rb.velocity.y.Equals(jumpHeight);
}
}
}
Controller Script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(FirstPerson3DMotor))]
public class FirstPerson3DController : MonoBehaviour {
//used to represent the speed the character walks.
[Header("Movement Settings")]
public float walkSpeed = 2.0f;
//Used to represent their speed while sprinting.
public float moveSpeed;
//Used to represent look sensitivity for the camera.
public float lookSensitivity = 10.0f;
//Used for setting the jump height
public float jumpHeight = 4f;
//Used to represent if the player can sprint or not.
//(Usage: if the player is hurt and they cannot sprint).
public bool canSprint = true;
//Used to represent weather the player's footsteps can be heard.
[Header("Sound Settings")]
public bool soundEnabled = true;
//used to represent the clip-clop sound of footsteps.
[SerializeField]
private AudioClip footStep1;
[SerializeField]
private AudioClip footStep2;
[Header("HUD Settings")]
public bool enableHUD = true;
public GameObject reticle;
public bool hideReticle = false;
[Header("Player Camera")]
public Camera playerCamera;
private FirstPerson3DMotor fpsMotor;
private bool isSprinting;
private bool isJumping;
private GameObject playerHud;
// Called when the game starts.
void Start () {
/*Setting FPS Motor equal to the the object this
scripts attached to's version of the FPS Motor Object. */
fpsMotor = GetComponent<FirstPerson3DMotor>();
playerHud = GetComponentInChildren<Canvas>().gameObject;
Cursor.visible = false;
HUDConfigure();
Physics.gravity = new Vector3(0, -20.0F, 0);
moveSpeed = walkSpeed * 1.5f;
}
void HUDConfigure()
{
if (enableHUD == false)
{
playerHud.SetActive(false);
}
if(hideReticle == true)
{
reticle.SetActive(false);
}
}
void HideCursorListener()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
Cursor.visible = true;
}
if (Input.GetMouseButtonDown(0))
{
Cursor.visible = false;
}
}
void CheckIfSprinting()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && canSprint == true)
{
isSprinting = true;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
isSprinting = false;
}
}
void CalculateMovementVelocity()
{
float xMove = Input.GetAxis("Horizontal");
float zMove = Input.GetAxis("Vertical");
Vector3 movHorizontal = transform.right * xMove;
Vector3 movVertical = transform.forward * zMove;
Vector3 velocity;
if (isSprinting == false)
{
velocity = (movHorizontal + movVertical) * walkSpeed;
}
else
{
velocity = (movHorizontal + movVertical) * moveSpeed;
}
fpsMotor.Move(velocity);
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.VelocityChange);
}
}
void CalculateRotation()
{
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
//Apply rotation
fpsMotor.Rotate(_rotation);
//Calculate camera rotation as a 3D vector (turning around)
float _xRot = Input.GetAxisRaw("Mouse Y");
float cameraRotation = _xRot * lookSensitivity;
//Apply camera rotation
fpsMotor.RotateCamera(cameraRotation);
}
void Update () {
CheckIfSprinting();
CalculateMovementVelocity();
CalculateRotation();
HideCursorListener();
}
}
Thanks in advance :)
Also disregard the is kinematic tick box being clicked I did that for debugging purposes.
ccsettings.png
(41.6 kB)
Comment