I’m new to Unity and I still don’t understand many things, any idea why this is happening?
I think the issue is that you’re adding transform.position.y
to the move vector. and I am assuming the y
is not zero when the character steps on something. In which case your finalMoveVector
becomes (0,1,0)
when normalized, and that causes your character to fly up.
Sure! there you go
using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private InputActionReference movement;
[SerializeField] private float moveSpeed = 7f;
public float speedMultiplier = 1f;
private Animator characterAnimator;
private CinemachineVirtualCamera virtualCamera;
private bool canRun;
private void Awake()
{
virtualCamera = GameObject.FindObjectOfType<CinemachineVirtualCamera>();
characterAnimator = GetComponentInChildren<Animator>();
}
private void Start()
{
virtualCamera.Follow = this.transform;
}
private void Update()
{
Vector2 moveVector = movement.action.ReadValue<Vector2>();
Vector3 finalMoveVector = new Vector3(moveVector.x, transform.position.y, moveVector.y);
finalMoveVector.Normalize();
if (finalMoveVector != Vector3.zero)
{
transform.forward = Vector3.Lerp(transform.forward, finalMoveVector, Time.deltaTime * 17f);
transform.position += (finalMoveVector * (moveSpeed * speedMultiplier)) * Time.deltaTime;
characterAnimator.SetBool("IsWalking", true);
}
else
{
characterAnimator.SetBool("IsWalking", false);
}
}
}
The character has this structure:
An empty object and then the actual visual part of the character
Also the rigidbody and the collider are in the “MaleCharacterPBR”, may that be the problem?