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;
}
}
}