I’m following the Brackeys first person movement tutorial, and when I go to jump, my movement locks up, and I get this error message:
transform.positionWithLocalOffset assign attempt for 'Player' is not valid. Input.positionWithLocalOffset is {NaN, NaN, NaN }. UnityEngine.CharacterController.Move(Vector3)
Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
float moveSpeed;
float walkingSpeed = 10f;
float crouchedSpeed = 5f;
public float jumpHeight = 5f;
public float mouseSens = 100f;
public float gravity = -14.715f;
public float groundDistance = 0.4f;
float xRotation;
public bool isCrouched;
public bool isGrounded;
public Transform playerCam;
public Transform playerBody;
public Transform groundCheck;
Vector3 normalScale;
Vector3 crouchScale;
Vector3 velocity;
public LayerMask ground;
public CharacterController playerController;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
normalScale = new Vector3(1.2f, 1.5f, 1.2f);
crouchScale = new Vector3(1.2f, 1f, 1.2f);
isCrouched = false;
moveSpeed = walkingSpeed;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ground);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Moved out of Move() while troubleshooting.
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
Look();
Move();
velocity.y -= gravity * Time.deltaTime;
playerController.Move(velocity * Time.deltaTime);
}
private void FixedUpdate()
{
}
private void Move()
{
float vertical = Input.GetAxis("Vertical") * moveSpeed;
float horizontal = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = transform.right * horizontal + transform.forward * vertical;
//Vector3 newPosition = playerRb.position + transform.TransformDirection(movement);
playerController.Move(movement * Time.deltaTime);
/*if (Input.GetKeyDown(KeyCode.LeftShift) && !isCrouched)
{
moveSpeed = crouchedSpeed;
transform.localScale = crouchScale;
isCrouched = true;
}
else if (Input.GetKeyDown(KeyCode.LeftShift) && isCrouched)
{
moveSpeed = walkingSpeed;
transform.localScale = normalScale;
isCrouched = false;
}*/
}
void Look()
{
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSens * Time.deltaTime;
float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSens * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerBody.Rotate(Vector3.up * mouseX);
playerCam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
Any help would be greatly appreciated!