Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Move to mouse location not working as intended

Discussion in 'Scripting' started by MrBAlderson, May 28, 2023.

  1. MrBAlderson

    MrBAlderson

    Joined:
    Mar 28, 2021
    Posts:
    3
    Edit: Issue resolved, the sphere component in shipGroup was not zeroed

    First, a disclaimer, I am pretty new to all of this and I apologise if my code/method is an absolute mess.

    I'm currently trying to implement a system where a player can click on a group of ships, and then right click where they would like them to go. There are three relevant gameobjects:
    • shipGroup: The item to be moved, with a script to detect when it has been clicked on
    • playerControl: Manager for all player resources, contains the script to manage commands being applied to groups
    • plane: A transparent x-z plane, position (0,0,0) used with rays to find mouse hit location.
    When a player clicks on a shipGroup object the following code relays which object has been clicked on to the script on playerManager:

    Code (CSharp):
    1.     public void OnMouseDown()
    2.     {
    3.         playerControl.GetComponent<ShipGroupControlScript>().shipGroupSelected = gameObject;
    4.         playerControl.GetComponent<ShipGroupControlScript>().issuingCommand = true;
    5.     }
    The script on playerControl then waits for a right click somewhere in the world, and uses raycasting to return where it has clicked, passing this info back to the shipGroup script:


    Code (CSharp):
    1. void Update()
    2.     {
    3.         mCamera = Camera.main;
    4.         if (issuingCommand)
    5.         {
    6.             if (Input.GetMouseButtonDown(1))
    7.             {
    8.                 Vector3 mousePosition = Input.mousePosition;
    9.                 Ray ray = mCamera.ScreenPointToRay(mousePosition);
    10.                 if (Physics.Raycast(ray, out RaycastHit hit))
    11.                 {
    12.                     if (hit.transform.gameObject == plane)
    13.                     {
    14.                         moveDestination.x = hit.point.x;
    15.                         moveDestination.y = 0;
    16.                         moveDestination.z = hit.point.z;
    17.                         shipGroupSelected.GetComponent<ShipGroupScript>().destinationCoords = moveDestination;
    18.                         shipGroupSelected.GetComponent<ShipGroupScript>().isMoving = true;
    19.                         shipGroupSelected = null;
    20.                     }
    21.                 }
    22.             }
    23.         }
    24.      
    25.      
    26.     }
    The shipGroup then manages the movement:


    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(isMoving == true)
    4.         {
    5.             if(transform.position == destinationCoords)
    6.             {
    7.                 isMoving = false;
    8.                 return;
    9.             }
    10.             else if(transform.position != destinationCoords)
    11.             {
    12.                 moveVector = (destinationCoords - transform.position).normalized;
    13.                 transform.position = transform.position + moveSpeed * moveVector * Time.deltaTime;
    14.             }
    15.         }
    16.     }
    Now this all works, other than the fact that the shipGroup moves to a position about 2-3 units above (in the z-axis) the place clicked. Can anybody see what I'm doing wrong here?
     
    Last edited: May 29, 2023