Search Unity

Why am I not able to move this object on the Z axis?

Discussion in 'Editor & General Support' started by Octo_Games, Jun 9, 2019.

  1. Octo_Games

    Octo_Games

    Joined:
    Jul 19, 2018
    Posts:
    4
    Recently, I’ve started work on grid-based placing game focused around earning money. I have almost all the basis’ of the game figured out, but there’s this one reoccurring issue — I can’t move objects around of the Z-axis. It works fine on the X-axis but won’t nudge on the Z. Can anyone help figure this out? Any and all help is appreciated.

    P.S: there are many scripts associated with this script, but the placing/moving lies within this script.

    EDIT: Changed currentBuilding to where it was originally before I tested to see if Z ever changes

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlaceableBuilding : MonoBehaviour
    6. {
    7.     private Transform currentBuilding;
    8.     private bool hasPlaced;
    9.     private BuildingManager bm;
    10.     public Transform grid;
    11.     public List<GameObject> border = new List<GameObject>();
    12.  
    13.     public float minX, maxX, minZ, maxZ;
    14.  
    15.     private void Start()
    16.     {
    17.         bm = GetComponent<BuildingManager>();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         if(currentBuilding != null && !hasPlaced)
    23.         {
    24.             Vector3 mouse = Input.mousePosition;
    25.             mouse = new Vector3(mouse.x, mouse.y, transform.position.y);
    26.             Vector3 pos = Camera.main.ScreenToWorldPoint(mouse);
    27.             currentBuilding.position = new Vector3(pos.x, transform.position.y, pos.y);
    28.  
    29.             if(Input.GetMouseButton(0))
    30.             {
    31.                 Vector3 p = new Vector3(currentBuilding.localPosition.x, currentBuilding.localPosition.y, currentBuilding.localPosition.z);
    32.  
    33.                 float nx = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).x);
    34.                 float ny = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).y);
    35.                 float nz = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).z);
    36.  
    37.                 Vector3 v = new Vector3 (nx, ny, nz);
    38.                 if (nx > maxX || nx < minX || nz > maxZ || nz < minZ)
    39.                 {
    40.                     Debug.LogError("Out of Bounds");
    41.                     Debug.Log(Camera.main.ScreenToWorldPoint(mouse));
    42.                 } else if (nx <= maxX || nx >= minX || nz <= maxZ || nz >= minZ)
    43.                 {
    44.                     hasPlaced = true;
    45.                 }
    46.             }
    47.         }
    48.     }
    49.  
    50.     public void SetItem(GameObject item)
    51.     {
    52.         hasPlaced = false;
    53.         currentBuilding = Instantiate(item, new Vector3(0, 0, 0), Quaternion.identity).transform;
    54.     }
    55. }
    56.  
     
    Last edited: Jun 10, 2019
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    This line prevents moving on Z:
    Code (CSharp):
    1.             currentBuilding.position = new Vector3(pos.x, 0, 2);
    Either change that 2 to something of value, or do not modify Z axis.
     
  3. Octo_Games

    Octo_Games

    Joined:
    Jul 19, 2018
    Posts:
    4
    I fixed it to where line 28 should be able to move around on the Z axis, but it just doesn't.