Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# using sprite gameobject as a cursor

Discussion in 'Scripting' started by Gibbons, Apr 14, 2014.

  1. Gibbons

    Gibbons

    Joined:
    Feb 28, 2013
    Posts:
    8
    Hello,

    I'm working on a top-down shooting game that uses nothing but 2D sprites, and I'm starting work on shooting/aiming for the game, and I need a cursor. It's a little crosshair that (later on) will be set up to animate, change its colour, and even disappear depending on certain conditions. I originally used Unity's own GUI solution with a certain degree of success, but I decided to make it part of the game world for various reasons, e.g. so it could be affected by the player camera's zoom level and I could achieve a meaty pixellated look.

    After experimenting and some googling, this is my code:

    Code (csharp):
    1.  
    2. public class PlayerCrosshair : MonoBehaviour
    3. {
    4.     private Vector2 mouse = new Vector2(0,0);
    5.     public float moveSpeed = 1f;
    6.    
    7.     void Start ()
    8.     {
    9.         Screen.showCursor = false;
    10.     }  
    11.    
    12.     void Update ()
    13.     {
    14.         CrosshairPosition();
    15.     }
    16.  
    17.     void CrosshairPosition()
    18.     {
    19.         mouse = Input.mousePosition;
    20.         mouse = Camera.main.WorldToScreenPoint(mouse);
    21.         transform.position = Vector2.Lerp(new Vector3 (transform.position.x , transform.position.y, -5f), mouse, moveSpeed * Time.deltaTime);
    22.     }
    23. }
    The script is attached to the crosshair object, and I want it to attach itself to the mouse's current position with a slight smooth transition between its old and new position.

    When I run the game, the crosshair seemingly disappears from the game, and this happens in the inspector:



    See the transform. The transform starts at (0,0,0) when everything's in the scene view, but then that happens whilst running.

    I can't imagine what I'm doing wrong in my script, but then again I'm newish to scripting. Could anyone lend me a little hand? The player's movement/turning/camera in my game is working wonderfully, but I can't get much further unless this is solved.
     
  2. Gibbons

    Gibbons

    Joined:
    Feb 28, 2013
    Posts:
    8
    UPDATE

    Please pardon the bump, but I've done some more fiddling and now the cursor works, but just look at this, this is bizarre:



    The cursor works exactly as I want it to (for now), but its only shows/works in the scene view and not in the game view. Why would this be?

    My new code below:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerCrosshair : MonoBehaviour
    6. {
    7.     private Vector3 mousePos;
    8.  
    9.     void Start ()
    10.     {
    11.         Screen.showCursor = false;
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         CrosshairPosition();
    17.     }
    18.  
    19.     void CrosshairPosition()
    20.     {
    21.         mousePos = Input.mousePosition;
    22.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    23.         transform.position = mousePos;
    24.     }
    25. }
    26.  
    Screen.showCursor = false; isn't the cause of my problems, I just tested it.
     
    Last edited: Apr 14, 2014
  3. mactinite77

    mactinite77

    Joined:
    Feb 14, 2012
    Posts:
    25
    It looks like the ScreenToWorld function isn't getting a full argument. You have to also pass it a Z value, or world units from the camera. I would change the mousePos variable assignment to something like
    Code (csharp):
    1.  
    2. float playerDistance = Vector3.Distance(Camera.main.position, player.position)
    3. mousePos = new Vector3(Input.mousePosition.x,Input.mousePosition.y,playerDistance);
    I'm doing this from work so there might be some discrepancies in the code.
     
    Last edited: Apr 14, 2014
  4. Gibbons

    Gibbons

    Joined:
    Feb 28, 2013
    Posts:
    8
    AAHA YES it works now!

    New code is as follows:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerCrosshair : MonoBehaviour
    5. {
    6.     private Vector3 mousePos;
    7.     private GameObject player;
    8.  
    9.     void Start ()
    10.     {
    11.         Screen.showCursor = false;
    12.         player = GameObject.FindWithTag("Player");
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         CrosshairPosition();
    18.     }
    19.  
    20.     void CrosshairPosition()
    21.     {
    22.         float playerDistance = Vector3.Distance(Camera.main.transform.position, player.transform.position);
    23.         mousePos = new Vector3(Input.mousePosition.x,Input.mousePosition.y,playerDistance);
    24.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    25.         transform.position = mousePos;
    26.     }
    27. }
    Thanks a bunch, man. Now I can finally make progress!

    Off to make a basic weapon/damage system and to learn about raycasting...
     
  5. mactinite77

    mactinite77

    Joined:
    Feb 14, 2012
    Posts:
    25
    No problem! Good luck!