Jittery movement

I probably should have asked this a few lectures ago but the movement of my spaceship is incredibly jittery. Is there anyway to fix this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float forceMagnitude;
    [SerializeField] float maxVelocity;

    Camera mainCamera;
    Rigidbody rigidBody;
    Vector3 movementDirection;
    

    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;
        rigidBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        movePlayer();
        KeepOnScreen();
        RotatePlayer();
    }

    private void FixedUpdate()
    {
        if (movementDirection == Vector3.zero)
        {
            return;
        }

        rigidBody.AddForce(movementDirection * forceMagnitude, ForceMode.Force);

        // This stops the velocity from going above the maxVelocity
        rigidBody.velocity = Vector3.ClampMagnitude(rigidBody.velocity, maxVelocity);
    }

    private void movePlayer()
    {
        if (Touchscreen.current.primaryTouch.press.isPressed)
        {
            Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();
            Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);

            movementDirection = worldPosition - transform.position;
            movementDirection.z = 0;
            movementDirection.Normalize();
        }
        else
        {
            movementDirection = Vector3.zero;
        }
    }

    private void KeepOnScreen()
    {
        Vector3 newPosition = transform.position;
        Vector3 viewportPosition = mainCamera.WorldToViewportPoint(transform.position);

        if (viewportPosition.x < 0)
        {
            newPosition.x = -newPosition.x - 0.1f;
        }
        else if (viewportPosition.y < 0)
        {
            newPosition.y = -newPosition.y - 0.1f;
        }
        else if (viewportPosition.x > 1)
        {
            newPosition.x = -newPosition.x + 0.1f;
        }
        else if (viewportPosition.y > 1)
        {
            newPosition.y = -newPosition.y + 0.1f;
        }

        transform.position = newPosition;
    }

    private void RotatePlayer()
    {
        transform.rotation = Quaternion.LookRotation(rigidBody.velocity, Vector3.back);
    }
}

I could be wrong, but it may be because you are moving using both transform and rigidbody. In the KeepOnScreen method, try setting rigidBody.position instead of transform.position. Not 100% sure that would help, though

1 Like

The course master appears to use transform.position = newPosition as well, but I suspect this is the solution. Non-kinematic Rigidbodies don’t like it when the transform is moved by another force such as a NavMeshAgent or transform.position =

1 Like

I had a look at the course repo and there are a few things misaligned:

In your FixedUpdate() you are missing Time.deltaTime in AddForce(). It should be

rigidBody.AddForce(movementDirection * forceMagnitude * Time.deltaTime, ForceMode.Force);

In your movePlayer() your moveDirection is not calculated in the same way, but it looks like it’s wrong in the repo. There we have

movementDirection = transform.position - worldPosition;

which is the direction from the touch to the player. The player will move away from the point where you touched. Not sure what this is about.

Your RotatePlayer() is going to snap to the rotation because it’s missing come code, mainly the Lerp that rotates it smoothly. The code should be

private void RotatePlayer()
{
    if (rigidBody.velocity == Vector3.zero) { return; }

    Quaternion targetRotation = Quaternion.LookRotation(rigidBody.velocity, Vector3.back);

    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * time.deltaTime);
}

Thanks mate.

The Time.deltaTime part I think is the section where it’s updated for a few seconds and he says it’s not necessary.

The next part, he has it set up to move away from the press, whereas I prefer it the other way around so I changed it.

I think I posted this just before the Rotate Player section was updated so mine is the same now. Thanks for looking into it :slight_smile:

It’s my pleasure.

I haven’t done this course, so I wasn’t sure why the movement direction is the complete opposite of yours. It makes sense now.

Thank you very much, this seems to have worked :slight_smile:

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms