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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Object moving for no reason

Discussion in '2D' started by Neomedus, Jun 22, 2020.

  1. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    Here are two scripts that I have, the first one finds if the object is in front and is attached to my gameplay manager, the second script clicks and drags the object and is attached to the object being dragged. In the second script I have when I check to see if the object is on top before I drag it I use this code: gameplayManager.top == this.gameObject

    My problem is when this piece of code is in my script, the object jumps up to the camera's z-value. If I delete this piece of code, the object doesn't do this however I can't check if the object is in front. Does anyone know why the z-value would be jumping to the camera when I am just checking to see if an object is in front?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Cast : MonoBehaviour
    6. {
    7.     private LayerMask clickableLayers;
    8.     private RaycastHit2D[] hits; //Change this number to however many selectable objects you think you'll have layered on top of eachother. This is for performance reasons.
    9.     private float rayStart = -1; //Start Raycast from this Z-Coordinate
    10.     private float rayEnd = 1;  //End Raycast at this Z-Coordinate
    11.     private GameObject selectedObject;
    12.     private GameplayManager gameplayManager;
    13.  
    14.     private void Awake()
    15.     {
    16.         gameplayManager = FindObjectOfType<GameplayManager>();
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         clickableLayers = (1 << LayerMask.NameToLayer("Default")); //Set the layers you want to be clickable here, duplicate the lines for however many you need
    22.                        // | (1 << LayerMask.NameToLayer("Layer1"))
    23.                       //  | (1 << LayerMask.NameToLayer("Default"));
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (Input.GetButtonDown("Fire1"))
    29.         {
    30.             SelectTopTile(); //This is how you can call it at any time
    31.         }
    32.        
    33.     }
    34.  
    35.     private void SelectTopTile()
    36.     {
    37.         Vector3 clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Store out clicked position
    38.         hits = Physics2D.LinecastAll(new Vector3(clickedPos.x, clickedPos.y, rayStart), new Vector3(clickedPos.x, clickedPos.y, rayEnd), clickableLayers); //Cast ray at the world space the mouse is at
    39.  
    40.         if (hits.Length > 0) //Only function if we actually hit something
    41.         {
    42.             int topHit = 0; //Set our top hit to a default of the first index in our "hits" array, in case there are no others
    43.             int preVal = hits[0].transform.GetComponent<SpriteRenderer>().sortingLayerID; //Store the SortingLayerID of the first object in the array, so it doesn't get skipped
    44.             int preSubVal = hits[0].transform.GetComponent<SpriteRenderer>().sortingOrder; //Store the SortingOrder value of the first object in the array, in case it needs to be compared to
    45.  
    46.             for (int arrayID = 1; arrayID < hits.Length; arrayID++) //Loop for every extra item the raycast hit
    47.             {
    48.                 int curVal = hits[arrayID].transform.GetComponent<SpriteRenderer>().sortingLayerID; //Store SortingLayerID of the current item in the array being accessed
    49.  
    50.                 if (curVal < preVal) //If the SortingLayerID of the current array item is lower than the previous lowest
    51.                 {
    52.                     preVal = curVal; //Set the "Previous Value" to the current one since it's lower, as it will become the "Previous Lowest" on the next loop
    53.                     topHit = arrayID; //Set our topHit with the Array Index value of the current closest Array item, since it currently has the highest/closest SortingLayerID
    54.                     preSubVal = hits[arrayID].transform.GetComponent<SpriteRenderer>().sortingOrder; //Store SortingOrder value of the current closest object, for comparison next loop if we end up going to the "else if"
    55.                 }
    56.                 else if ((curVal == preVal)&&(hits[arrayID].transform.GetComponent<SpriteRenderer>().sortingOrder > preSubVal)) //If SortingLayerID are the same, then we need to compare SortingOrder. If the SortingOrder is lower than the one stored in the previous loop, then update values
    57.                 {
    58.                     topHit = arrayID;
    59.                     preSubVal = hits[arrayID].transform.GetComponent<SpriteRenderer>().sortingOrder;
    60.                 }
    61.             }
    62.             selectedObject = hits[topHit].transform.gameObject;
    63.             gameplayManager.TopObj(selectedObject);
    64.         }
    65.        
    66.     }
    67. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class ClickDrag : MonoBehaviour
    7. {
    8.  
    9.     private Vector3 screenPoint;
    10.     private Vector3 offset;
    11.     private GameObject act;
    12.     private string active;
    13.     private GameplayManager gameplayManager;
    14.     private SpriteRenderer sprite;
    15.     private int sprLayer;
    16.     private void Awake()
    17.     {
    18.         gameplayManager = GameObject.FindObjectOfType<GameplayManager>();
    19.     }
    20.     private void Start()
    21.     {
    22.         sprite = gameObject.GetComponent<SpriteRenderer>();
    23.  
    24.         active = gameObject.name;
    25.     }
    26.  
    27.     void OnMouseDown()
    28.     {
    29.        
    30.         if (gameplayManager.top == this.gameObject)
    31.         {
    32.             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    33.             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    34.         }
    35.     }
    36.     void OnMouseDrag()
    37.     {
    38.         Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    39.         Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
    40.         gameObject.transform.position = cursorPosition;
    41.     }
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I would say to drop in a debug line about what your offset gets computed to be. I suspect it is coming out to the camera's z and that's where you're getting the move of the z from. If you want to avoid that, probably only want to change the x and y of the transform position instead of including the z element.
     
  3. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    ye your screenPoint.z is the camera's z
     
    Neomedus likes this.
  4. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    I removed the offset and it still does it.
     
  5. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    I changed screenpoint.z to 0 and it still does it.
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    what if you do

    Code (CSharp):
    1.      gameObject.transform.position = new vector3(cursorPosition.x, cursorPosition.y, gameObject.transform.position.z);
    2.    
     
    Neomedus likes this.
  7. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    It works. Thank you.
     
    raarc likes this.