- Home /
Isometric game, player look at cursor?
Hi! I'm going crazy trying to figure out how to make my player (a simple cube) always look at the cursor. I've tried a few things, including various modifications to suggestions in this post.
My basic setup is an isometric game with a cube character. Here is a screenshot of the setup:
My character, the cube, has the ability to move along the X and the Z axis - he will always be locked in the same Y coordinate (sitting on the flat ground). My attempts have mainly been using something similar to this:
void Start()
{
cameraDif = camera.transform.position.y - player.transform.position.y;
}
void Update ()
{
player.transform.LookAt(new Vector3(Input.mousePosition.x, cameraDif, Input.mousePosition.z));
}
Any help would be greatly appreciated! Thanks :)
Hi Alap Soni, I've experimented with that a fair bit and had no success. $$anonymous$$y code was this:
private float cameraDif;
public Rigidbody player;
private float mouseX;
private float mouseZ;
private Vector3 worldPos;
void Start()
{
cameraDif = camera.transform.position.y - player.transform.position.y;
}
void Update ()
{
mouseX = Input.mousePosition.x;
mouseZ = Input.mousePosition.z;
worldPos = camera.ScreenToWorldPoint(new Vector3(mouseX, cameraDif, mouseZ));
player.transform.LookAt(worldPos);
}
It's almost exactly the same as the code used in one of the comments in the thread here. Have I done something wrong/adapted it to my situation incorrectly, or will it just not work in my situation?
Have you by any chance tried raycasting, then using the LookAt function (or something similar) to make your cube rotate and face towards the raycast's hit?
In the link you provided in your initial post, one of the first responses uses that method, so I was just curious whether or not you'd already given it a shot.
There is a great article that caters around the Isometric game development which shows how to convert 2D view (screen co-ordinates) to 2.5D something along the line of what you want: Creating Isometric Worlds: A Primer for Game Developers
Chubzdoomer, I've tried to decipher that code you're referring to, but could not. I am quite new to coding, and struggle to understand JS at this point in time. If possible, could you help translate it to C#? Still struggling with this.
Answer by Chubzdoomer · Oct 09, 2014 at 07:51 PM
Here's a quick script I wrote that seems to do the trick. All it does is perform a raycast from the main camera to the mouse cursor and beyond. If the raycast strikes anything you've tagged as "Ground," the cube will then rotate to face towards the point of the hit.
public class CubeScript : MonoBehaviour
{
Ray cameraRay; // The ray that is cast from the camera to the mouse position
RaycastHit cameraRayHit; // The object that the ray hits
void Update ()
{
// Cast a ray from the camera to the mouse cursor
cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
// If the ray strikes an object...
if (Physics.Raycast(cameraRay, out cameraRayHit))
{
// ...and if that object is the ground...
if(cameraRayHit.transform.tag=="Ground")
{
// ...make the cube rotate (only on the Y axis) to face the ray hit's position
Vector3 targetPosition = new Vector3(cameraRayHit.point.x, transform.position.y, cameraRayHit.point.z);
transform.LookAt(targetPosition);
}
}
}
}
I'm sure the code to be improved in several ways (like making the rotation occur over time rather than instantly), but this should at least help get you started.
I hope this helps, and best of luck!
I'm currently doing something similar, is there a noticeable performance issue if you remove the "need" for the function to hit the "ground" and have the sprite constantly face the mouse, since it will be updating to the mouse position consistently?
Answer by TheShadyColombian · Oct 14, 2014 at 09:23 PM
try the method they use in the Nightmares tutorial in Unite 2014. you can find the tutorial in youtube in the Unity channel.
Answer by BarthaSzabolcs · May 02, 2021 at 10:55 AM
I made a tutorial about this, it's basically the same solution, but I try to explain how it works in the video.
I will leave it here, might help somebody. :)