I have a project where I combine 2D and 3D elements. The project itself is 3D and when I try to move my player it jitters. I tried using fixedDeltaTime and tried to useLateUpdate but none of them worked. I don't know what I did wrong. It was working fine at first but when I added the camera script and played with colliders it became like this.
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private float smoothSpeed = 0.125f;
[SerializeField] private Vector3 offset;
private Vector3 zero = Vector3.zero;
private void FixedUpdate()
{
Vector3 desiredPosition = target.position offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
transform.LookAt(target);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody rb;
private Animator anim;
[SerializeField] private float hDirection;
[SerializeField] private float vDirection;
[SerializeField] private Collider coll;
[SerializeField] private float hSpeed = 0.5f;
[SerializeField] private float vSpeed = 0.5f;
private enum State { idle, running, turnRight, turnLeft};
State state = State.idle;
void Start()
{
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
void Update()
{
Move();
}
private void Move()
{
hDirection = Input.GetAxis("Horizontal");
vDirection = Input.GetAxis("Vertical");
if (hDirection > 0)
{
transform.position = transform.right * Time.fixedDeltaTime * hSpeed;
}
if (hDirection < 0)
{
transform.position -= transform.right * Time.fixedDeltaTime * hSpeed;
}
if (vDirection > 0)
{
transform.position = transform.forward * Time.fixedDeltaTime * vSpeed;
}
}
}
CodePudding user response:
You are using a physical body (Rididbody) for your player, but you are moving it with transform.position
. This is not the correct way. You either need to control your rb
with MovePosition(), or change the velocity of it. Or you can use CharacterController and its methods for movement.
Just remember - don't use transform
for physical movement.
EDIT: also, remember that movements I've described above must be inside FixedUpdate()
, not just Update()
.
CodePudding user response:
I changed the camera script for you, Vector3.Lerp() takes two points and a decimal between 0-1, this decimal represents a position between the two endpoints. The left endpoint is 0 and the right endpoint is 1. 0.5 returns the endpoint between the two points. Bring the rigTransform closer to the target position in a gradual and slow manner. That is - the camera will follow the player character. Back to Unity. Select Main Camera in the structure view, in the inspector, the Target is Player
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
[SerializeField] private Transform rigTransform;
[SerializeField] public GameObject target;
[SerializeField] public float smoothSpeed = 0.125f;
void Start(){
target = this.transform.parent;
}
void FixedUpdate()
{
if(target == null){
return;
}
rigTransform.position = Vector3.Lerp(rigTransform.position,
target.transform.position,
Time.deltaTime * smoothSpeed);
}
}