- Home /
Help with rigidbody character controller
So I'm working on a rigidbody character controller for a stealth platformer, similar to Sly Cooper. I've got some basic movement going, but the major two problems with it are 1: Whenever I stop moving, my character's rotation is reset to 0 2: The character moves in the direction the camera is facing no matter what, meaning that when the camera is pointed upwards my character will run into the sky.
Here's my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Controllers/Player Controller")]
public class PlayerController : MonoBehaviour {
public Animator playerAnimator;
Transform cameraPos;
CapsuleCollider col; //Collider
Rigidbody phys; //Rigidbody
float BaseHeight;//The default height of the character controller
public float CrouchingHeight; //The height of the character controller while crouching
public float RunSpeed = 5.0f;
public float SprintSpeed = 10.0f;
float CurrentSpeed = 0.0f;
bool Grounded = true;
// Use this for initialization
void Start () {
cameraPos = Camera.main.transform;
col = GetComponent<CapsuleCollider> ();
phys = GetComponent<Rigidbody> ();
BaseHeight = col.height;
}
// Update is called once per frame
void Update () {
playerAnimator.SetFloat ("GroundSpeed", CurrentSpeed);
playerAnimator.SetBool ("Grounded", Grounded);
//Movement
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Vector3 movementDir = new Vector3 (h, 0f, v);
movementDir = cameraPos.TransformDirection (movementDir);
transform.forward = movementDir;
if (Grounded) {
CurrentSpeed = RunSpeed * movementDir.magnitude;
phys.velocity = movementDir * CurrentSpeed;
}
}
}
Any help with this is appreciated. Thanks in advance.
On the rotation issue, I noticed this pops up in the console when I stop moving
Look rotation viewing vector is zero
UnityEngine.Transform:set_forward(Vector3)
PlayerController:Update() (at Assets/Scripts/Controllers/PlayerController.cs:45)
The camera I'm using for this is just the standard free look camera.
I fixed the rotation issue, all i had to do was add if (movementDir != Vector3.zero) before setting transform.forward
EDIT: Now I am having an issue where my character starts spinning when they stop moving.
As far as the movement issue goes, it only happens when moving forward and backward; my character stays aligned to the ground while moving left or right.
Here is the updated script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponent$$anonymous$$enu("Controllers/Player Controller")]
public class PlayerController : $$anonymous$$onoBehaviour {
public Animator playerAnimator;
Transform cameraPos;
CapsuleCollider col; //Collider
Rigidbody phys; //Rigidbody
float BaseHeight;//The default height of the character controller
public float CrouchingHeight; //The height of the character controller while crouching
public float RunSpeed = 5.0f;
public float SprintSpeed = 10.0f;
float CurrentSpeed = 0.0f;
//Jump variables
public float $$anonymous$$axJumpHeight = 1.5f;
//State variables
public float GroundCheckDistance = 0.1f; //How close to the ground is the player to be considered grounded?
public float WallCheckDistance = 0.3f; //How close to a wall can the player be to start climbing?
bool Grounded ()
{
return Physics.Raycast (transform.position, -Vector3.up, GroundCheckDistance);
}
// Use this for initialization
void Start () {
cameraPos = Camera.main.transform;
col = GetComponent<CapsuleCollider> ();
phys = GetComponent<Rigidbody> ();
BaseHeight = col.height;
}
// Update is called once per frame
void Update () {
playerAnimator.SetFloat ("GroundSpeed", CurrentSpeed);
playerAnimator.SetBool ("Grounded", Grounded());
playerAnimator.SetFloat ("AirSpeed", phys.velocity.y);
//$$anonymous$$ovement
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Vector3 movementDir = new Vector3 (h, 0f, v);
movementDir = cameraPos.TransformDirection (movementDir);
if (movementDir != Vector3.zero)
transform.forward = movementDir;
if (Grounded()) {
//$$anonymous$$ovement
CurrentSpeed = RunSpeed * movementDir.magnitude;
phys.velocity = movementDir * CurrentSpeed;
//Jumping
if (Input.GetButtonDown ("Jump")) {
phys.AddForce (Vector3.up * $$anonymous$$axJumpHeight, Force$$anonymous$$ode.Impulse);
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to limit an object's position to a spherical radius? 0 Answers
Collider/Rigidbody issue.Camera shaking! 3 Answers