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

Updated on June 10, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on June 9, 2019

Hello all,

 

 

 

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

 

 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
 
public class PlaceableBuilding : MonoBehaviour
{
 private Transform currentBuilding;
 private bool hasPlaced;
 private BuildingManager bm;
 public Transform grid;
 public List border = new List();
 
 
 
public float minX, maxX, minZ, maxZ;
 
 
 
private void Start()
 {
 bm = GetComponent();
 }
 
 
 
private void Update()
 {
 if(currentBuilding != null && !hasPlaced)
 {
 Vector3 mouse = Input.mousePosition;
 mouse = new Vector3(mouse.x, mouse.y, transform.position.y);
 Vector3 pos = Camera.main.ScreenToWorldPoint(mouse);
 currentBuilding.position = new Vector3(pos.x, transform.position.y, pos.y);
 
 
 
if(Input.GetMouseButton(0))
 {
 Vector3 p = new Vector3(currentBuilding.localPosition.x, currentBuilding.localPosition.y, currentBuilding.localPosition.z);
 
 
 
float nx = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).x);
 float ny = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).y);
 float nz = Mathf.Round(Camera.main.ScreenToWorldPoint(mouse).z);
 
 
 
Vector3 v = new Vector3 (nx, ny, nz);
 if (nx > maxX || nx < minX || nz > maxZ || nz < minZ)
 {
 Debug.LogError("Out of Bounds");
 Debug.Log(Camera.main.ScreenToWorldPoint(mouse));
 } else if (nx <= maxX || nx >= minX || nz <= maxZ || nz >= minZ)
 {
 hasPlaced = true;
 }
 }
 }
 }
 
 
 
public void SetItem(GameObject item)
 {
 hasPlaced = false;
 currentBuilding = Instantiate(item, new Vector3(0, 0, 0), Quaternion.identity).transform;
 }
}
 
 
 
  • Liked by
Reply
2 on June 10, 2019

I think it’s because at line 28, you’re only passing in the pos.x, and forcing Y to 0, and Z to 2.

 

Instead of using screen to world point, try using ScreenPointToRay (Unity docs here) and set currentBuilding.position to the raycast hit point of that ray.

 

To lock them to the grid, instead of free-form placement, you can set it to the grid tiles position that the ray cast hit, and then just offset it by some set amount to keep everything in line.

on June 10, 2019

Where I load 2 into Z, that should be pos.y, I did that to see if Z changes

on June 10, 2019

Also, I have a SnapToGrid script on the object that is being placed, so no worries there 😀

Show more replies
  • Liked by
Reply
Cancel