I'm trying to make a building system where you can stack things on top of eachother, so I'm using a raycast that goes in front of the player and from the sky to determine where the block should be, but for some reason, the block goes down and then back up and just repeats that, it's weird. Here's a video to better describe it https://imgur.com/a/D74NiXm Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlaceBlocks : MonoBehaviour { public GameObject selectedBlock; public AudioClip placeSound; public LayerMask placeLayermask; bool isPlacing = true; GameObject block = null; private void Update() { if ( block == null ) { block = Instantiate(selectedBlock); } else if ( isPlacing ) { Ray ray = new Ray(transform.position + transform.forward * 5 + transform.up * 30, Vector3.down); if ( Physics.Raycast(ray, out RaycastHit hit, 1000, placeLayermask) ) { block.transform.position = hit.point; } } } public void Select() { isPlacing = true; } }
I am not 100% sure, but you spawn cubes in the ground, not on the ground. If you put rigidbody inside other collider then physics engine will try to repel them so they don't intersect. Try to spawn cubes on the ground or a bit above.
I don't think it's that because the cube don't have a rigidbody attached, they're just a normal cube with a material applied. I tried what you said anyway but it didn't work.
Well, I need to base everything on data you provided - your cube might have script incrementing Y position every frame, but I do not know that. Anyway, in unity objects does not move without reason, so it either has rigidbody with velocity or there is script that translates position of this cube.
I figured it out, the ray was hitting the cube it was placing, which led to it continiously moving the cube on top of itself, causing it to look like it was going up.
Right, just to explain what a solution may be: Use a layermask for your raycast. Put either the ground or the cubes on a seperate layer so you can selectively ignore / choose which layers you want the raycast to hit and which should be ignored.