Help converting a character controller script to rigidbody
So I'm currently working on a 3D game in which the main character is a cube. The issue is that the script I've been trying to make work for my movement uses the character controller, and the character controller capsule collider is causing quite a few problems. My idea was that whatever way the camera is facing is forwards for the character. I've been trying to shift the script from utilizing the character controller to utilizing rigidbodies so I can use a cube collider, but I can't get it to work. if anyone had any advice or tips, that would be great. Thanks! Here's the script with all the character controller pieces in it:
using UnityEngine;
using System.Collections;
public class CharacterControls : MonoBehaviour {
[SerializeField]
private CharacterController controller;
public float deadzone = 0.25f;
public float turnSpeed = 800.0f;
public float jumpForce = 3.0f;
public float gravity = 10.0f;
public float speed = 8.0f;
public bool jump = false;
private Vector3 moveDir = Vector3.zero;
private float lockRotation = 0.0f;
void Start ()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
// Is the controller on the ground?
if (controller.isGrounded)
{
jump = false;
// Assigning Translation Controls
moveDir = new Vector3(Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical"));
// Align forward vector of movement and camera. Turn it off when a camera isn't assigned.
moveDir = Camera.main.transform.rotation * moveDir;
// Rotation parameters based on stick direction
if(moveDir.sqrMagnitude > deadzone)
{ transform.rotation = Quaternion.LookRotation(moveDir); }
//Jump
if (Input.GetButtonDown("Jump"))
{
moveDir.y = jumpForce;
jump = true;
}
while (jump == false);
}
// Applying gravity variable to the character controller when jumping
moveDir.y -= gravity * Time.deltaTime;
// Applying move direction to the character controller
controller.Move(moveDir * speed * Time.deltaTime);
}
void LateUpdate()
{
// Lock rotations x and z to stop tilting.
controller.transform.rotation = Quaternion.Euler(lockRotation, transform.rotation.eulerAngles.y, lockRotation);
}
}
Answer by AsclepiiusUnknown · Apr 11, 2020 at 12:14 AM
Did you ever figure it out @matthewgustafani ????
Your answer
Follow this Question
Related Questions
Player still move left&right in MenuUI 0 Answers
Control speed of a rigidbody that move to a fixed distance 2 Answers
rigidbody movement is not smooth 0 Answers
Converting my Character Controller Movement to Rigidbody 1 Answer
Should i be using rigidbody or CharacterController for Controlling the player? 1 Answer