Single Raycast affecting performance

I’m attempting to get some movement going for a cube on a plane. The camera has been adjusted to replicate the look of an isometric game. The movement is just with wasd and the cube rotates to look at the mouse. This all works except it seems laggy and also the cube will not move while the mouse is being moved.

I’m using a raycast onto a plane to get the angle to look at. Is there something I’m missing about how screenPoinToRay works? Performance shouldn’t be an issue with a single raycast, even if it’s every frame.

Any help would be amazing!

using UnityEngine;

public class movementTest3D : MonoBehaviour
{ 
    [SerializeField] private Rigidbody _rb;
    [SerializeField] private float _speed = 5;
    [SerializeField] Camera cam;
    public float planeSize = 10f;
    Plane playerPlane;
    private Vector3 _input;
    private Vector3 lookRotation;

    private void Start()
    {
        playerPlane = new Plane(Vector3.up, transform.position);
        lookRotation = Vector3.zero;
        cam = Camera.main;
    }
    void FixedUpdate()
    {
        transform.LookAt(lookRotation);
        _rb.MovePosition(_rb.position + _input * _speed * Time.deltaTime);

    }

    private void Update()
    {
        getInput();
    }

    void getInput()
    {
        _input = new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal"));


        Vector3 mouseScreenPosition = Input.mousePosition;

        Ray ray = cam.ScreenPointToRay(mouseScreenPosition);

        float enter = 0.0f;
        if (playerPlane.Raycast(ray, out enter) && Input.GetMouseButton(1))
        {

            Vector3 hitPoint = ray.GetPoint(enter);

            //transform.LookAt(hitPoint, Vector3.up);
            lookRotation = hitPoint;
            // Quaternion targetRotation = Quaternion.LookRotation(direction);
            // transform.rotation = targetRotation;

        }

    }
}

transform.LookAt won’t be smooth when used from FixedUpdate because it updates at 50hz. FixedUpdate is for physics stuff and transform.LookAt isn’t really for physics.

MovePosition is for kinematic rigidbodies and so if your cube isn’t kinematic then it’s movement won’t be interpolated and therefore not smooth. Also, MovePosition isn’t really blocked by collisions and so you should use AddForce instead.

using UnityEngine;
public class movementTest3D : MonoBehaviour
{ 
    [SerializeField] private Rigidbody _rb;
    [SerializeField] private float _speed = 5;
    [SerializeField] Camera cam;
    public float planeSize = 10f;
    Plane playerPlane;
    private Vector3 _input;
    private Vector3 lookAt;

    private void Start()
    {
        playerPlane = new Plane(Vector3.up, transform.position);
        cam = Camera.main;
        _rb.drag=2;
    }
    void FixedUpdate()
    {
        _rb.AddForce(_input * _speed);
    }

    private void Update()
    {
       _rb.MoveRotation(Quaternion.LookRotation(lookAt - transform.position, Vector3.up));
        getInput();
    }

    void getInput()
    {
        _input = new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal"));
        Vector3 mouseScreenPosition = Input.mousePosition;
        Ray ray = cam.ScreenPointToRay(mouseScreenPosition);
        if (playerPlane.Raycast(ray, out float enter) && Input.GetMouseButton(1))
        {
            Vector3 hitPoint = ray.GetPoint(enter);
            lookAt = hitPoint;
        }
    }
}
1 Like

Appreciate it! Working now. I’m still confused why the movement was only being affected while the mouse was in motion. Even really choppy they should still both work since position and rotation are independent right?

I didn’t really test your script and so I’m not sure. You may be able to improve things further by changing from a cube to a capsule and then there’ll be fewer contact points and therefore less friction. And a capsule won’t snag on sharp corners.