Hi everyone, I’m very new to Unity but am excited to try working on a 3D Platformer/Collectathon.
I originally gave a rigidbody to my character and added raycasting to limit the times he could jump, but I decided to transition to a character controller. I like the CharacterController.Move script (Unity - Scripting API: CharacterController.Move ), but it uses the horizontal axis for strafing, and I wanted to use it for rotation instead. Here’s how I modified it. (Note that I allow the character to rotate in the air.)
It’s working alright for me so far, but if you have any questions or suggestions feel free to share them. Again, I’m very new to Unity and programming!
using UnityEngine;
using System.Collections;
public class CharacterControllerScript : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float rotateSpeed = 3.0F;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
CharacterController controller = GetComponent();
transform.Rotate(0, Input.GetAxis(“Horizontal”) * rotateSpeed * Time.deltaTime, 0);
if (controller.isGrounded)
{
moveDirection = new Vector3(0, 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton(“Jump”))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}