Search Unity

unity Mouse Follow/Camera Screen Point to Ray Confusion

Discussion in 'Scripting' started by laurenmhowie, Sep 25, 2018.

  1. laurenmhowie

    laurenmhowie

    Joined:
    Jun 10, 2017
    Posts:
    29
    Hello,

    I am confused as to why my game cursor follow script does not seem to perform differently based on the character's position. I want the mouse to have to be over the character's arm and I thought that was how the script was working until I moved my character along the X axis and found that the location the mouse had to move around in order for the game object to copy its position stayed near the game object's original position even as the gameobject and camera moved away. Would anyone have any idea why? I've pasted in the script for finding the Mouse's position and the script for having my game object copy the mouse's position.

    Thanks for your time!

    Code (CSharp):
    1. public static class GameCursor
    2. {
    3.  
    4.     public static LayerMask mask;
    5.  
    6.  
    7.     public static Vector2 WorldPosition
    8.     {
    9.         get
    10.         {
    11.             float distance;
    12.             int mask = 1<<9; //setting layer mask to layer 9
    13.             mask = ~mask; //setting the layer mask to ignore layer(~)
    14.                 //adding in the layermask to raycast will ignore 9th layer
    15.  
    16.  
    17.        
    18.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.             Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.black);
    20.             if (_gamePlane.Raycast(ray, out distance))
    21.                 return ray.GetPoint(distance);
    22.  
    23. return Vector2.zero;
    24.        
    25.        
    26.  
    27.  
    28.         }
    29.     }
    30.     private static Plane _gamePlane = new Plane(Vector3.forward,0);
    31.  
    32.  
    33. }
    34.  
    35.  

    Code (CSharp):
    1.  
    2.     public GameObject ArmR;
    3.  
    4.  
    5.     public static Vector2 OffsetPoint;
    6.  
    7.     public Vector2 OffsetVec2;
    8.  
    9.     void Start ()
    10.  
    11.     {
    12.  
    13.     }
    14.  
    15.  
    16.     private void Update()
    17.     {
    18.  
    19.         if (Input.GetKey (KeyCode.E)) {
    20.  
    21.  
    22.             Follow ();
    23.         }
    24.  
    25.         if (Input.GetKeyUp (KeyCode.E)) {
    26.  
    27.  
    28.         }
    29.  
    30.         //print (transform.eulerAngles.z);
    31.         //Offset.transform.position = OffsetVec2;
    32.         OffsetPoint = GameCursor.WorldPosition + OffsetVec2;
    33.  
    34.         Vector3 clampedRotation = transform.eulerAngles;
    35.         // Clamp the z value
    36.         if (clampedRotation.z > 90.0f) clampedRotation.z = 180.0f - clampedRotation.z;{
    37.             clampedRotation.z = Mathf.Clamp (clampedRotation.z, 0, 90);
    38.             clampedRotation.z = Mathf.Clamp (clampedRotation.z, -90, 180);
    39.  
    40.         }
    41.    
    42.         // assign the clamped rotation
    43.         ArmR.transform.rotation = Quaternion.Euler(clampedRotation);
    44.  
    45.  
    46.         //if(ArmR.transform.eulerAngles.z >= 350 || transform.eulerAngles.z <= 270){
    47.  
    48.             //Debug.Log ("no");
    49.  
    50.  
    51.  
    52.         //}
    53.  
    54.         if(ArmR.transform.eulerAngles.z == 0){
    55.  
    56.             //Debug.Log ("AT0");
    57.  
    58.         }
    59.  
    60.         if(ArmR.transform.eulerAngles.z == 90){
    61.  
    62.             //Debug.Log ("AT90");
    63.        
    64.  
    65.  
    66.  
    67.         }
    68.  
    69.  
    70.         }
    71.  
    72. void Follow(){
    73.  
    74.         ArmR.transform.up = -GameCursor.WorldPosition + OffsetVec2;
    75.     }
    76.  
    77.     private void OnDrawGizmos()
    78.     {
    79.         Gizmos.DrawIcon(GameCursor.WorldPosition, "wallll", true);
    80.  
    81.     }
    82. }
    83.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're changing your OffsetPoint based on the cursor's world position, but I imagine you want the mouse position more in screen space if your character is moving along with the camera? As your world position grows, your OffsetPoint will grow too.