- Home /
Making the collider change after being instantiate
I am making a building system, pretty much like in the game rust. Basically building mechanics. But the problem is when I place down the object, it still doesn't have the collider, so I can't build on top of since the ray cast does not hit the object, because it doesn't have the working collider. Basically what I want to do is when I place the object, my collider is on when it's just showing the preview the collider is off. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Placement : MonoBehaviour
{
public Collider ObjectCollider;
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private KeyCode newObjectHotKey = KeyCode.F;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void Update()
{
HandleNewObjectHotkey();
if (currentPlaceableObject != null)
{
MovecurrentPlaceableObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
private void RotateFromMouseWheel()
{
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
}
private void MovecurrentPlaceableObjectToMouse()
{
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 HandleNewObjectHotkey()
{
if (Input.GetKeyDown(newObjectHotKey))
{
if (currentPlaceableObject == null)
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
{
}
}
else
{
Destroy(currentPlaceableObject);
}
}
}
}
Answer by ccwiegreff · Mar 25, 2020 at 06:52 PM
Whats the object in question? If the mesh collider is convex you might have an error that a collider was never made.
Answer by RunDidle · Mar 25, 2020 at 07:26 PM
It's just a box collider. And the object is just a cube platform. @ccwiegreff