Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Placing objects on top of each other problem (solved)

Discussion in 'Scripting' started by LodestoneNetwork, Jul 25, 2018.

  1. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    OK, so I need to be able to place objects on top of other objects with raycasting. I am able to place an object on top of the first object, but I cant place an object an object on top of the second object.

    Place where object is instantiated

    Code (CSharp):
    1.  
    2. if(Input.GetKeyDown(KeyCode.Mouse1) && logs != 0) {
    3.                 if(logDirection == false) { // logDirection is a bool because I would be able to toggle it
    4.                     Instantiate(logUp,logPlacePos,Quaternion.identity);
    5.                     logs--;
    6.                 }
    7.                 if(logDirection == true) {
    8.                     Instantiate(logSide,new Vector3(logPlacePos.x,logPlacePos.y+1,logPlacePos.z),Quaternion.Euler(0, gameObject.transform.eulerAngles.y, 0)); // gameObject.transform.eulerAngles.y is so the object is rotated like the player, logSide is a 90 degree rotated log, logUp is a log that stands up.
    9.                     logs--;
    10.                 }
    11.             }
    12.  
    Raycasting script

    Code (CSharp):
    1.  
    2. public class Raycasting : MonoBehaviour {
    3.    
    4.     public Vector3 pos;
    5.     public Camera camera;
    6.     float MaxDistance = 20;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void FixedUpdate () {
    15.         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    16.         RaycastHit hit;
    17.         if(Physics.Raycast(camera.transform.position,camera.transform.forward,out hit,MaxDistance)) {
    18.             if(hit.transform.name == "Terrain") {
    19.                 print(hit.point);
    20.                 pos = hit.point;
    21.             }
    22.         }
    23.     }
    24. }
    25.  
    Freeze object script (This is because sometimes the object falls, so when it hits the ground, it freezes in place. The logs have a collider under them that detects if it hit something. "rb" is a "public Rigidbody rb")

    Code (CSharp):
    1.  
    2. void OnTriggerEnter (Collider col){
    3.         if(col.gameObject.tag != "Player") { // to make sure it doesnt freeze on top of the player
    4.             rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX;
    5.            
    6.         }
    7.     }
    8.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    What happens when you try?
     
  3. KdotDevelopment

    KdotDevelopment

    Joined:
    Oct 2, 2016
    Posts:
    26
    It would like go inside of the second object, but you would still see it. Either that or it went behind the logs.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I don't know if it's the problem, but you do realise that the logPlacePos doesn't increase when you say logPlacePos.y+1

    it doesn't increase the y value, it spits out the y value +1.

    you need to add logPlacePos.y += 1 before/after the instatiating call.

    Code (CSharp):
    1.  Instantiate(logSide,new Vector3(logPlacePos.x,logPlacePos.y+1,logPlacePos.z),Quaternion.Euler(0, gameObject.transform.eulerAngles.y, 0)); // gameObject.transform.eulerAngles.y is so the object is rotated like the player, logSide is a 90 degree rotated log, logUp is a log that stands up.
    2.                     logs--;
     
  5. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    i use y+1 because the object will otherwise be half in the terrain, and does not affect anything of this matter.

    The point of the game is to place the logs where ever in a 3d world, and stack them to make a house...
     
  6. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
  7. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    sorry to bump again, but i really need help
     
  8. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Your casting your ray into the scene and spawning the game object at that location pointed to by the camera.

    What you need to do is find out if there is something above the point hit by the camera ray.

    I would suggest after you get the camera ray intersection point you use a box/sphere/capsule overlap test to see if there is something blocking the creation of the next block. If there is, do a loop that checks for an intersection with thr overlap volume again a few units above that one, and keep doing it until space is found for the object, or max iterations is reached.
     
  9. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    oh ok, thanks!!