- Home /
Question by
AtomicDev · Jun 09, 2019 at 11:57 PM ·
c#movement scriptgrid based gamenotworking
Why does the object not move on the Z-Axis?
I asked this on the Forum, but this is a better place to ask. This is a placement script for moving objects for a grid-based placing game that is focused on earning money. It's working perfectly fine, except for the fact that it only moves on the X-axis and not the Z-axis. Does anyone have a solution?
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<GameObject> border = new List<GameObject>();
public float minX, maxX, minZ, maxZ;
private void Start()
{
bm = GetComponent<BuildingManager>();
}
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, 0, 2);
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;
}
}
Comment
Your answer
Follow this Question
Related Questions
How can i add movement distance into my code?,how can i add distance into this movement code? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player Diagonal Velocity 1 Answer
score counter is not accurate 1 Answer