I have a c# script which works to place objects anywhere on the ground plane by clicking the left mouse button, I also have it so the right mouse button will rotate the object 90 degrees - however when i try to rotate it on the ground plane it won’t work. It glitches and will not stay in the rotated position (only when you hover the cursor on the very edge of the ground plane will the object stay rotated the way it i want it too)
Can anyone help?
I
using UnityEngine;
public class GroundPlacement : MonoBehaviour
{
[SerializeField]
private GameObject PlaceObject;
[SerializeField]
private KeyCode NewObjectKey = KeyCode.Z;
private GameObject CurrentObject;
private void Update()
{
HandleNewObjectKey();
if (CurrentObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseClick();
ReleaseIfClicked();
}
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
CurrentObject = null;
}
}
private void RotateFromMouseClick ()
{
if (Input.GetMouseButtonDown(1))
{
CurrentObject.transform.Rotate(0, 90, 0);
}
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit HitInfo;
if (Physics.Raycast (ray, out HitInfo))
{
CurrentObject.transform.position = HitInfo.point;
CurrentObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, HitInfo.normal);
}
}
private void HandleNewObjectKey()
{
if (Input.GetKeyDown(NewObjectKey))
if (CurrentObject == null)
{
CurrentObject = Instantiate(PlaceObject);
}
else
{
Destroy(CurrentObject);
}
}
}