Below is my Character Controller script. Currently the character rotates towards the mouse and I can move it using WASD in relation to the mouse position. (ie. W moves the character towards the mouse, S moves character away from mouse). I can strafe left and right along the players X axis, however I can’t seem to get it to move forward/backwards as well as left/right. Essentially I have no 45deg angles. Let me know if you need anymore information.
using UnityEngine;
public class PlayerController03 : MonoBehaviour
{
public float rotateSpeed = 0.2f;
public float walkSpeed = 3f;
public float walkBackwardsSpeed = 2f;
public float runSpeed = 6f;
public float speedSmoothTime = 0.1f;
public float gravity = -12f;
public float jumpHeight = 1f;
Animator animator;
Camera cam;
CharacterController controller;
private Vector2 input;
private Vector2 inputDir;
bool aiming;
bool running;
float currentSpeed;
float speedSmoothVelocity;
Vector3 velocity;
float velocityY = 0f;
void Start()
{
controller = GetComponentInChildren<CharacterController>();
animator = GetComponentInChildren<Animator>();
cam = Camera.main;
}
void Update()
{
//maps raw input to normalized vector2
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
inputDir = input.normalized;
//Grabs aiming and running
aiming = Input.GetMouseButton(1);
running = Input.GetKey(KeyCode.LeftShift);
//gets the current speed of the player depending on whether they're running or walking and dampens
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
//activates aiming animation
if(aiming){
animator.SetBool("isAiming", true);
}else{
animator.SetBool("isAiming", false);
}
if(Input.GetKey(KeyCode.Space)){
Jump();
}
//creates a ray to the current mouse position and rotates the player towards it
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
int layer_mask = LayerMask.GetMask("Ground");
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100, layer_mask)){
Vector3 targetDir = hit.point - transform.position;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, rotateSpeed, 0.0f);
if(inputDir != Vector2.zero || aiming)
transform.rotation = Quaternion.LookRotation(new Vector3(newDir.x, 0, newDir.z));
}
//sets the initial velocity, but this is when the player is standing still, but allows to jump in place
velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
//sets the velocity of the player based on input
if(inputDir == Vector2.up)
velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
if(inputDir == Vector2.down)
velocity = -transform.forward * currentSpeed + Vector3.up * velocityY;
if(inputDir == Vector2.right)
velocity = transform.right * currentSpeed + Vector3.up * velocityY;
if(inputDir == Vector2.left)
velocity = -transform.right * currentSpeed + Vector3.up * velocityY;
if(inputDir == Vector2.zero){
velocity = Vector3.Lerp(velocity, Vector3.zero, speedSmoothTime);
}
// Debug.Log(velocity);
}
void FixedUpdate(){
Debug.Log(velocity);
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
velocityY += Time.deltaTime * gravity;
if(controller.isGrounded){
velocityY = 0;
}
}
void Jump(){
if(controller.isGrounded){
float jumpVelocity = Mathf.Sqrt(-2*gravity * jumpHeight);
velocityY = jumpVelocity;
}
}
}