Search Unity

Resolved Why does this happen?

Discussion in 'Scripting' started by jlorenzi, May 31, 2022.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlaceBlocks : MonoBehaviour
    6. {
    7.     public GameObject selectedBlock;
    8.     public AudioClip placeSound;
    9.     public LayerMask placeLayermask;
    10.  
    11.     bool isPlacing = true;
    12.     GameObject block = null;
    13.  
    14.     private void Update()
    15.     {
    16.         if ( block == null )
    17.         {  
    18.             block = Instantiate(selectedBlock);
    19.         }
    20.  
    21.         else if ( isPlacing )
    22.         {
    23.             Ray ray = new Ray(transform.position + transform.forward * 5 + transform.up * 30, Vector3.down);
    24.  
    25.             if ( Physics.Raycast(ray, out RaycastHit hit, 1000, placeLayermask) )
    26.             {
    27.                 block.transform.position = hit.point;
    28.             }
    29.         }
    30.     }
    31.  
    32.     public void Select()
    33.     {
    34.         isPlacing = true;
    35.     }
    36. }
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,307
    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.
     
    Bunny83 likes this.
  3. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    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.
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,307
    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.
     
  5. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    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.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    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.