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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Instantiate gameobject on mouseclick from the top down perspective

Discussion in 'Scripting' started by Oddinx, Jun 24, 2019.

  1. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Hi, I'm making a 3d top down game and I want my character instantiate gameobject on mouse click.

    The problem is that when I create a game object the gameobject is not created in the position I want, the object is created in the air and not in surface of the floor. This is the code:


    Game object in game:

    Sin título.png Sin título1.png


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InstantiateCube : MonoBehaviour
    6. {
    7.  
    8.  
    9.  
    10.  
    11.     public GameObject cube;
    12.     // Start is called before the first frame update
    13.  
    14.  
    15.  
    16.    public void Spawn(Vector3 position){
    17.  
    18.  
    19. Instantiate(cube).transform.position = position;
    20.  
    21.  
    22.    }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.      
    28. if(Input.GetKeyDown(KeyCode.Mouse0)){
    29.  
    30. Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition,Camera.MonoOrStereoscopicEye.Mono);
    31.  
    32. Vector3 adjustZ = new Vector3(worldPoint.x,worldPoint.y,cube.transform.position.z);
    33.  
    34. Spawn(adjustZ);
    35.  
    36.  
    37.  
    38. }
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.     }
    46. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Use Physics.Raycast to cast ray from camera through mouse pointer. When ray hits the ground, create the object on that position.
     
    Oddinx likes this.
  3. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Ok that worked,thanks. I have another problem. When I instantiate a new object some objects overlap others. How I fix that?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testclick : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.    public GameObject cube;
    9.     // Start is called before the first frame update
    10.   Ray myRay;
    11.  
    12. RaycastHit hit;
    13.  
    14.  
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20. myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.  
    22. if(Physics.Raycast(myRay,out hit)){
    23.  
    24. if(Input.GetMouseButtonDown(0)){
    25.  
    26. Instantiate (cube,hit.point,Quaternion.identity);
    27.  
    28.  
    29.  
    30.  
    31. }
    32.  
    33.  
    34.  
    35.  
    36. }
    37.  
    38.  
    39.     }
    40. }
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    That depends on your game logic. In general, you may check if colliders overlap or use OnTriggerEnter to disable placement if there are obstacles for instance.