- Home /
Problem is not reproducible or outdated
Weird Placement Glitch
Hi, I have got this glitch that has been bugging me for days and I really haven't found the answer to fixing it . The bug goes as follows, I press play in unity then hit a hotkey to enable placement of trees on a plane but them it sort of phases in and out and glitches between placing and destroying, this makes it nearly impossible to place the asset. (BTW this is only to create a game mechanic so I have a base to build on)
The script: using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic;
public class GroundPlacementController : MonoBehaviour
{
public List<GameObject> trees = new List<GameObject>();
private GameObject placeableObjectPrefab;
private bool isToggled = false;
public Button firTree;
public Button oakTree;
public Button palmTree;
public Button poplarTree;
private int i = 0;
[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;
[SerializeField]
private KeyCode cycleObjectHotKey = KeyCode.Tab;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private void Start()
{
Button fTB = firTree.GetComponent<Button>();
fTB.onClick.AddListener(TreeZero);
Button oTB = oakTree.GetComponent<Button>();
oTB.onClick.AddListener(TreeOne);
Button pTB = palmTree.GetComponent<Button>();
pTB.onClick.AddListener(TreeTwo);
Button ppTB = poplarTree.GetComponent<Button>();
ppTB.onClick.AddListener(TreeThree);
}
private void Update()
{
placeableObjectPrefab = trees[i];
if (Input.GetKeyDown(newObjectHotkey))
{
isToggled = true;
}
HandleNewObjectBool();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
if (Input.GetKeyDown(cycleObjectHotKey))
{
i++;
print(placeableObjectPrefab);
if (i == 4)
{
i = i * 0;
}
}
}
private void HandleNewObjectBool()
{
if (isToggled == true)
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
}
}
private void PlaceNewObject()
{
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
private void RotateFromMouseWheel()
{
Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
private void TreeZero()
{
i = 0;
}
private void TreeOne()
{
i = 1;
}
private void TreeTwo()
{
i = 2;
}
private void TreeThree()
{
i = 3;
}
}
In all honesty I am a beginner at coding and I feel like its a pretty obvious mistake. Thank you.
There is nothing preventing it from destroying the object and then re-creating it on the next frame, many times a second.
Could you explain exactly how you want it to function? Are you supposed to position the mouse and then press the mouse button once? Are you supposed to be able to press and drag the mouse to place multiple trees? Or is that supposed to simply drag the newest tree around until the mouse button is released? etc. Go into details, please.
Follow this Question
Related Questions
Grey Screen? 2 Answers
Editor Camera moving while rotating 0 Answers
Changing Skybox tint in game, permanently changes it in editor? 1 Answer
Sprites Leaving Trails Glitch? 0 Answers
Why is this code glitching? 2 Answers