Hi. I’ve been following on the unity learn premium:
Course
Advanced Fundamentals: Unity Game Dev Course
Project
Unity Gameplay Programming Fundamentals
And I’m having some troubles smothing the player movment for some reason. I’m using exact same code brought by the instructor.
The code itself changes the rigidbody.velocity to a value based on player input and camera output.
-
The character moves as expected but the rigidbody.velocity keeps resetting to “0” even when getting constant input by the player.
-
While the jitter is barley noticeable for the movment it affects badly any camera following and the animator that changes animations form idle to running based on the rigidbody.velocity.
Things that I already tried:
- Using the same package as the tutorial( same models, game objects, scenes).
- A constant rigidbody.velocity free from player input for input testing.
Any insights or directions for study?
public class Movement : MonoBehaviour
{
Rigidbody rg;
public int speed;
Vector3 movmentVelocity;
Animator anim;
private Camera mainCamera;
Vector3 test;
// Start is called before the first frame update
void Start()
{
rg = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
mainCamera = Camera.main;
test = new Vector3(3, 0, 0);
}
// Update is called once per frame
void Update()
{
//input
float h;
float v;
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Vector3 movmentInput = new Vector3(h, 0f, v);
//camera and rotation logic
Vector3 cameraForward = mainCamera.transform.forward;
cameraForward.y = 0;
Quaternion relativeRotation = Quaternion.FromToRotation(Vector3.forward, cameraForward);
Vector3 lookTowards = relativeRotation * movmentInput;
if (movmentInput.sqrMagnitude>0)
{
Ray lookRay = new Ray(transform.position, lookTowards);
transform.LookAt(lookRay.GetPoint(1));
}
movmentVelocity = transform.forward * speed * movmentInput.sqrMagnitude;
Animate();
}
private void FixedUpdate()
{
movmentVelocity.y = rg.velocity.y;
rg.velocity = movmentVelocity;
//rg.velocity = test;
}
void Animate()
{
anim.SetFloat("Speed", rg.velocity.sqrMagnitude);
}
}