Question by
unity_xG1qQ2GUpg7VjQ · Jul 02, 2020 at 10:56 AM ·
raycastraycasting3rd person camera
Difficulty getting a raycast from camera to work
This piece of code in VS2017:
Ray rayFromCameraToCursor = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane playerPlane = new Plane(Vector3.up, transform.position);
playerPlane.Raycast(rayFromCameraToCursor, out float distanceFromCamera);
Vector3 cursorPosition = rayFromCameraToCursor.GetPoint(distanceFromCamera);
Is meant to be a way of getting my 3D "player" object to face where the cursor is on the screen, hence the transform position. The camera is stationary, and the raycasting is supposed to hit the playerPlane that's at the same horizontal position as the player, so then the game can be like "Oh, ok you're looking at this specific part of the ray, and not above or below it".
The tutorial I'm following has this exactly as I've typed it, and I cannot for the life of me work out why it's working for them, but not me.
I'm not sure if maybe the
cursorPosition
variable is the problem, considering it's not assigned a meaning in the code?
Comment