- Home /
Need to Find Distance Between Mouse Cursor and Object in a 2.5D SideScroller
I'm totally new to Unity though I've been coding a bit and I've made some stuff in GameMaker. Anyways I've been working on the basic stuff for a 2.5D sidescroller and I'm having trouble with refining the camera. The cursor will move independent of the camera, but WILL extend the camera somewhat subtly if pushed near the edge. I'm also trying to get the camera to extend to the left or the right of the character depending on which direction the character is facing. I've got basic camera movement and camera boundaries relative to the character. I just need help with subtle camera movements and set ups based on the cursor in relation to the character (i.e. a cursor to the right of a character means he's facing that direction and would have the camera extend that direction). I'm having trouble properly measuring the distance between the cursor and the character.
To my understanding, "Raycasts" might be the best way to figure this out, but after messing with them a while I'm quite lost on them. I'm not really good with coding and I'm used to C++ so I've been using C#. It'd be awesome for anyone to get back to me!
Answer by robertbu · Jul 10, 2013 at 06:15 AM
The simplest method of making the conversion is to use Camera.ScreenToWorldPoint(). As a starting point, you will need to provide or calculate the distance between the camera and the plane you are using for your 2D movements. The code would then be something like this (assumes the camera is looking towards positive 'Z'):
Vector3 v3Pos = new Vector3(Input.mousePosition.x Input.mousePosition.y, distance);
v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
v3Pos = v3Pos - character.position;
if (v3Pos.x > 0.0f)
Debug.Log("Cursor is in front of the character");
else
Debug.Log("Cursor is behind the character");
Thanks! This got me a little farther, but I still can't manage to get it working.
using UnityEngine;
using System.Collections;
public class SideScroller$$anonymous$$ouse : $$anonymous$$onoBehaviour {
//$$anonymous$$ouse sensitivity
public float sensitivity = 1.0F;
//Default camera resting offsets, should be about two-thirds of the hard bounds
public float xRest = 1.4F;
public float yRest = 0.6F;
void Update () {
Vector3 camTemp = Camera.mainCamera.transform.position;
Vector3 transTemp = GameObject.Find("construction_worker").transform.position;
Vector2 cursTemp = new Vector2(Input.GetAxis("$$anonymous$$ouse X"), Input.GetAxis("$$anonymous$$ouse Y"));
Vector3 cursor = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0.0F);
cursor = Camera.main.ScreenToWorldPoint(cursor);
if (cursor.x >= transTemp.x)
{
if (cursor.x < transTemp.x + xRest)
camTemp.x = transTemp.x + xRest;
else if (cursor.x > transTemp.x + xRest)
camTemp.x -= cursTemp.x * sensitivity;
}
else if (cursor.x < transTemp.x)
{
if (cursor.x > transTemp.x - xRest)
camTemp.x = transTemp.x - xRest;
else if (cursor.x < transTemp.x - xRest)
camTemp.x -= cursTemp.x * sensitivity;
}
camTemp.y += cursTemp.y * sensitivity;
Camera.mainCamera.transform.position = camTemp;
}
}
There's my terrible code if you want to see. The camera does lock into position on either side of the character, but X mouse movement always moves the camera (rather than just when those two "else if" statements trigger) resulting in terrible jitter when I move the cursor towards the character. The camera also seems to switch sides only when I quickly move the mouse, rather than when I actually move it over the character.
I did try by subtracting the mouse world position by the characters transform as well:
cursor = cursor - transTemp;
And I edited all the if statements to match appropriately, but had no luck. Any further help would be awesome, and again thanks for the response.
Ohp. Sorry, I forgot to get the distance from the 2D plane to the camera. The plane is at 0 so I just got the Camera's Z position.
Vector3 cursor = new Vector3(Input.mousePosition.x, Input.mousePosition.y,camTemp.z);
The camera doesn't constantly move with X mouse movement, so the boundaries seem to work, but the camera still won't switch sides unless I move the mouse X axis quickly (rather than position the cursor on the other side of the character).
I take a look when I have more time this evening. If you are using the default setup with the camera looking towards positive 'z', then your distance will be '-camTemp.z'.
Well I'll continue working on this code, I'm definitely getting close, and you did answer my question and helped me out a lot. So thanks again!
Your answer
Follow this Question
Related Questions
Camera distance from mesh not center 1 Answer
Check if position is on screen 0 Answers
Locking cursor, while moving camera. 0 Answers
Screen.lockCursor messes my rotation 1 Answer
Large scales, increased clipping planes, and performance 1 Answer