When I attached my camera to my characters head bone so the camera moves up and down with the character it made it so when I move the character he is able to walk/run on any axis, basically he is able to fly. He can walk up down left right underground or in mid-air. I have two sets of code:
CameraControl Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camMouseLook : MonoBehaviour
{
public Transform characterTransform;
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 = characterTransform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
characterTransform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
characterTransform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
characterTransform.localEulerAngles = new Vector3(-rotationY, characterTransform.localEulerAngles.y, 0);
}
}
}
Character Control Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
private float speed = 0;
public float wSpeed;
public float rSpeed;
static Animator anim;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
anim = GetComponent<Animator>();
}
void Update()
{
float z = Input.GetAxis("Vertical") * speed;
float y = Input.GetAxis("Horizontal") * speed;
transform.Translate(0, 0, z);
transform.Translate(y, 0, 0);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
if (Input.GetKey(KeyCode.W))
{
speed = rSpeed;
anim.SetBool("isWalk", false);
anim.SetBool("isIdle", false);
anim.SetBool("isRun", true);
}
if (Input.GetKey("left shift") && Input.GetKey(KeyCode.W))
{
speed = rSpeed;
anim.SetBool("isWalk", false);
anim.SetBool("isIdle", false);
anim.SetBool("isRun", true);
}
else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A) && !Input.GetKeyUp("left shift"))
{
speed = wSpeed;
anim.SetBool("isWalk", true);
anim.SetBool("isIdle", false);
anim.SetBool("isRun", false);
}
//Idle
else
{
anim.SetBool("isWalk", false);
anim.SetBool("isIdle", true);
anim.SetBool("isRun", false);
}
}
}
Thanks for any help!