When I press 'w' my character more like jumps away on top, same things with 'a' and 'd', but character moves left or right. It seems like a bug, but I've got two error messages:
NullReferenceException: Object reference not set to an instance of an object Gamekit3D.PlayerController.Awake () (at Assets/3DGamekitLite/Scripts/Game/Player/PlayerController.cs:149)
NullReferenceException: Object reference not set to an instance of an object Gamekit3D.PlayerController.OnDisable () (at Assets/3DGamekitLite/Scripts/Game/Player/PlayerController.cs:172)
here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharacter : MonoBehaviour
{
public Animator animator;
public Rigidbody playerPhysics;
public float jumpPower = 12f;
public float groundCheckDistance = 0.3f;
private bool isGrounded;
public float groundCheckDistanceSaved;
private float turnAmount;
public float forwardAmount;
private void Start()
{
groundCheckDistanceSaved = groundCheckDistance;
}
public void Move(Vector3 move, bool jump)
{
move = transform.InverseTransformDirection(move);
move = Vector3.ProjectOnPlane(move, Vector3.up);
turnAmount = Mathf.Atan2(move.x, move.z);
forwardAmount = move.z;
isGrounded = Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out _, groundCheckDistance);
if (isGrounded)
{
HandleGroundedMovement(jump);
}
else
{
HandleAirborneMovement();
}
ApplyExtraTurnRotation();
UpdateAnimator();
}
void UpdateAnimator()
{
animator.SetFloat("Forward", forwardAmount, 0.1f, Time.deltaTime);
animator.SetFloat("Turn", turnAmount, 0.1f, Time.deltaTime);
animator.SetBool("OnGround", isGrounded);
animator.SetFloat("Jump", playerPhysics.velocity.y);
}
void HandleAirborneMovement()
{
playerPhysics.AddForce(Physics.gravity);
groundCheckDistance = playerPhysics.velocity.y < 0 ? groundCheckDistanceSaved : 0.01f;
}
void HandleGroundedMovement(bool jump)
{
if (!jump || !isGrounded)
return;
playerPhysics.velocity = new Vector3(playerPhysics.velocity.x, jumpPower, playerPhysics.velocity.z);
isGrounded = false;
groundCheckDistance = 0.1f;
}
void ApplyExtraTurnRotation()
{
var turnSpeed = Mathf.Lerp(180, 360, forwardAmount);
transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime, 0);
}
public void OnAnimatorMove()
{
if (!isGrounded || Time.deltaTime < 0.001f)
return;
var velocity = (animator.deltaPosition) / Time.deltaTime;
velocity.y = playerPhysics.velocity.y;
playerPhysics.velocity = velocity;
}
}
I got asked for posting for player controller script, here's part of this script:
// Called automatically by Unity when the script first exists in the scene.
void Awake()
{
m_Input = GetComponent<PlayerInput>();
m_Animator = GetComponent<Animator>();
m_CharCtrl = GetComponent<CharacterController>();
meleeWeapon.SetOwner(gameObject);
s_Instance = this;
}
// Called automatically by Unity after Awake whenever the script is enabled.
void OnEnable()
{
SceneLinkedSMB<PlayerController>.Initialise(m_Animator, this);
m_Damageable = GetComponent<Damageable>();
m_Damageable.onDamageMessageReceivers.Add(this);
m_Damageable.isInvulnerable = true;
EquipMeleeWeapon(false);
m_Renderers = GetComponentsInChildren<Renderer>();
}
// Called automatically by Unity whenever the script is disabled.
void OnDisable()
{
m_Damageable.onDamageMessageReceivers.Remove(this);
for (int i = 0; i < m_Renderers.Length; ++i)
{
m_Renderers[i].enabled = true;
}
}
// Called automatically by Unity once every Physics step.
void FixedUpdate()
{
CacheAnimatorState();
UpdateInputBlocking();
EquipMeleeWeapon(IsWeaponEquiped());
m_Animator.SetFloat(m_HashStateTime, Mathf.Repeat(m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime, 1f));
m_Animator.ResetTrigger(m_HashMeleeAttack);
if (m_Input.Attack && canAttack)
m_Animator.SetTrigger(m_HashMeleeAttack);
CalculateForwardMovement();
CalculateVerticalMovement();
SetTargetRotation();
if (IsOrientationUpdated() && IsMoveInput)
UpdateOrientation();
PlayAudio();
TimeoutToIdle();
m_PreviouslyGrounded = m_IsGrounded;
}