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

Need Help with 2D top down shooter

Discussion in '2D' started by BuggedPixel, Feb 17, 2016.

  1. BuggedPixel

    BuggedPixel

    Joined:
    Feb 17, 2016
    Posts:
    10
    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
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CrosshairBehavior : MonoBehaviour {
    5.  
    6.      public Camera mainCam;     //The main camera for the scene. Set in inspector
    7.      public float camZOffset;     //How far away from the camera should the crosshair be set? Set in inspector
    8.  
    9.      void Update()     //In each frame...
    10.      {
    11.  
    12.           Vector3 cursorPos;
    13.           Vector3 offsCursorPos;
    14.  
    15.           cursorPos = Input.mousePosition;     //Get the cursor position on the screen
    16.           offsCursorPos = new Vector3(cursorPos.x, cursorPos.y, camZOffs);     //Offset the cursor position in the Z direction
    17.  
    18.           transform.position = mainCam.ViewportToWorldPoint(offsCursorPos);     //Move the crosshair to the transformed point
    19.      }
    20. }
    Or in JavaScript (UnityScript):

    Code (JavaScript):
    1. var mainCam : Camera;     //The main camera for the scene. Set in inspector
    2. var camZOffset : float;     //How far away from the camera should the crosshair be set? Set in inspector
    3.  
    4.      Update()     //In each frame...
    5.      {
    6.  
    7.           var cursorPos : Vector3;
    8.  
    9.           cursorPos = Input.mousePosition;     //Get the cursor position on the screen
    10.           cursorPos.z = camZOffset;     //Offset the cursor position in the Z direction
    11.  
    12.           transform.position = mainCam.ViewportToWorldPoint(cursorPos);     //Move the crosshair to the transformed point
    13.      }
    14. }
    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.
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CrosshairBehavior : MonoBehaviour {
    4.     void Start(){
    5.         Screen.showCursor = false;
    6.     }
    7.  
    8.      void Update()     //In each frame...
    9.      {
    10.           transform.position = Input.mousePosition;
    11.      }
    12. }
     
  4. BuggedPixel

    BuggedPixel

    Joined:
    Feb 17, 2016
    Posts:
    10
    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.
     
  5. BuggedPixel

    BuggedPixel

    Joined:
    Feb 17, 2016
    Posts:
    10
    actually i think the crosshair is invisible
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.