- Home /
2D platformer cursor
I am making a 2D platform shooter, and i need help with a crosshair script.
#pragma strict
var pos;
var crosshair : Transform;
function Start () {
pos = Input.mousePosition;
crosshair.position = pos;
Cursor.visible = false;
}
This is the code i have right now, i wanted to do it like this because of my camera script:
#pragma strict
var player:Transform;
var mouse:Transform;
var pos:Vector2;
var offSet = Vector3(0,0,10);
function Start ()
{
transform.position=player.position-offSet;
}
function Update ()
{
pos=(player.position + mouse.position) / 2;
transform.position.x=pos.x;
transform.position.y=pos.y;
}
function Update () {
}
The the camera script works is it grabs two transforms, the player and the cursor. So in order for this to work i need a cursor transform to be already in the scene and just move it to the mouse position. But the script doesn't seem to be working, i think it needs something to do with ScreenToWorldPoint, but i don't know how to use it. Can someone please help me with it. Thanks.
Answer by flopshotQQ · Jul 17, 2015 at 12:05 AM
using UnityEngine; using System.Collections;
public class Crosshair : MonoBehaviour {
private Vector3 MouseCoords;
public float MouseSensitivity = 0.1f;
void Update () {
GameObject crosshair = GameObject.Find ("crosshair");
MouseCoords = Input.mousePosition;
MouseCoords = Camera.main.ScreenToWorldPoint (MouseCoords);
crosshair.transform.position = Vector2.Lerp (transform.position, MouseCoords, MouseSensitivity);
print (MouseCoords);
}
}
Create a cross-hair prefab and use its name in the brackets here:
GameObject crosshair = GameObject.Find ("crosshair");
Your answer
Follow this Question
Related Questions
Shooting in direction of mouse cursor 2d 5 Answers
How to make a 2D character points his gun to the mouse position? 2 Answers
How to center custom cursor 2 Answers
2D Aiming with a mouse angle problem 1 Answer
SetCursor is not a member of Cursor 1 Answer