Hey this might have been answered in previous posts but im not sure. i am new to scripting and am having a bit of trouble. I am making a top down horde style shooter where the character is controlled and so is a cross-hair. i need to figure out the script to attack the cross-hair to the mouse cursor. any help would be greatly appreciated
There is a function to transform a point on the screen to a point in world space (read Camera.ViewportToWorldPoint() in the manual). You’ll need to get the position of the cursor using Input.mousePosition, then use to above function to transform that position into world space:
using UnityEngine;
using System.Collections;
public class CrosshairBehavior : MonoBehaviour {
public Camera mainCam; //The main camera for the scene. Set in inspector
public float camZOffset; //How far away from the camera should the crosshair be set? Set in inspector
void Update() //In each frame...
{
Vector3 cursorPos;
Vector3 offsCursorPos;
cursorPos = Input.mousePosition; //Get the cursor position on the screen
offsCursorPos = new Vector3(cursorPos.x, cursorPos.y, camZOffs); //Offset the cursor position in the Z direction
transform.position = mainCam.ViewportToWorldPoint(offsCursorPos); //Move the crosshair to the transformed point
}
}
Or in JavaScript (UnityScript):
var mainCam : Camera; //The main camera for the scene. Set in inspector
var camZOffset : float; //How far away from the camera should the crosshair be set? Set in inspector
Update() //In each frame...
{
var cursorPos : Vector3;
cursorPos = Input.mousePosition; //Get the cursor position on the screen
cursorPos.z = camZOffset; //Offset the cursor position in the Z direction
transform.position = mainCam.ViewportToWorldPoint(cursorPos); //Move the crosshair to the transformed point
}
}
This script would go on your crosshair object, and you would need to drag the main camera object onto mainCam in the inspector. You can also specify how far away from your camera the cursor would go in camZOffs.
This may not be perfect depending on your setup, because technically screen space is different from viewport space, and there may be differences in functionality depending on whether you are in windowed or fullscreen… But this should be a good starting point.
Disclaimer: may not be free of errors, since I’m away from my home computer and can’t test these. Someone please chime in if there are errors.
I would suggest a different approach than the above.
I think since the crosshair is really a UI element, you should just use the screen position directly to control the crosshair in a UI Overlay Canvas.
Create a Canvas object, set to Overlay in the Canvs component. Canvas Scalar component set to “Scale With Screen SIze”. Create an “Image” child object under the Canvas in the scene, set the image to your crosshair sprite, with “Preserve Aspect Ratio” set.
Then you can add a script to the crosshair (just altering the above script):
using UnityEngine;
using System.Collections;
public class CrosshairBehavior : MonoBehaviour {
void Start(){
Screen.showCursor = false;
}
void Update() //In each frame...
{
transform.position = Input.mousePosition;
}
}
Ok i think this works except i cant test it because no matter how i set the camera or crosshair in play view it is never seen.
actually i think the crosshair is invisible
I just tested this code, and it’s working correctly. You must have something with your canvas set up incorrectly.
Would you mind posting a snippet of your hierarchy setup for the crosshair? and inspector settings for the canvas and image?
Also, I just realized “Screen.showCursor” is deprecated. Use “Cursor.visible” instead.