YouTube Unity Tutorial I have been following:
I also got inspiration from this person’s character in their DevLog:
(They made a character within Unity and animated it as well…)
I have gone back and watched the video and I don’t see anything wrong…
(I can’t tell because I don’t know coding or Unity that well…)
Issues I’m having with my current Unity Project (SEE VIDEO):
1.) The character can move around freely, but things aren’t working properly (SEE CODE BELOW)
My character is made with in Unity (I don’t know how to use Blender…)
My character uses “Character Controller”
The character moves in the direction of the opposite the desired key
- A = Goes Right
- W = Goes Backward
- S = Goes Backwards
- D = Goes Left
(The character starts acting jittery when pressing the S key)
2.) This might be influenced by the “Camera Controller”
The camera also has some “resistance” to it…
(I don’t how else to describe it…)
“Player Controller Script”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
//public Rigidbody RB;
public float jumpForce;
public CharacterController controller;
public bool isGrounded;
private Vector3 moveDirection;
public float gravityScale;
public Animator anim;
public Transform pivot;
public float rotateSpeed;
public GameObject playerModel;
// Start is called before the first frame update
void Start()
{
//RB = GetComponent<Rigidbody>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
/* RB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, RB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
if (Input.GetButtonDown("Jump"))
{
RB.velocity = new Vector3(RB.velocity.x, jumpForce, RB.velocity.z);
} */
moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
moveDirection.y = yStore;
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirection * Time.deltaTime);
//Move the player in different directions based where on the camera is looking
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
anim.SetBool("isGrounded", controller.isGrounded);
anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
}
}
“Camera Controller Script”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public bool useOffsetValues;
public float rotateSpeed;
public Transform pivot;
public float maxViewAngle;
public float minViewAngle;
public bool invertY;
// Start is called before the first frame update
void Start()
{
if (!useOffsetValues)
{
offset = target.position - transform.position;
}
pivot.transform.position = target.transform.position;
//pivot.transform.parent = target.transform;
pivot.transform.parent = null;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void LateUpdate()
{
pivot.transform.position = target.position - offset;
//Get the X position of the mouse and rotate the target
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
pivot.Rotate(0, horizontal, 0);
//Get the Y position of the mouse and rotate the pivot
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
pivot.Rotate(vertical, 0, 0);
if(invertY)
{
pivot.Rotate(vertical, 0, 0);
}else
{
pivot.Rotate(-vertical, 0, 0);
}
//Limit the up/down camera rotation
if(pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
{
pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
}
if(pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
{
pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
}
//Move the camera based on the current rotation of the target and the original offset (X Axis)
float desiredYAngle = pivot.eulerAngles.y;
//Move the camera based on the current rotation of the target and the orignal offset (Y Axis)
float desiredXAngle = pivot.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
transform.position = target.position - (rotation * offset);
//transform.position = target.position - offset;
if (transform.position.y < target.position.y)
{
transform.position = new Vector3(transform.position.x, target.position.y -.5f, transform.position.z);
}
transform.LookAt(target);