Question by
adibak · Jul 07, 2021 at 02:19 AM ·
positioninstallationmouseclick
How to place instantiated prefab on ground?
Hi,
I have this code which basically creates a tree in front of the player when the user clicks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnTrees : MonoBehaviour
{
public GameObject tree;
public float distance;
void Start(){
}
//Vector3 playerPos = transform.position;
//Vector3 playerDirection = transform.forward;
float spawnDistance = 10;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)){
Vector3 playerPos = transform.position;
Vector3 playerDirection = transform.forward;
Vector3 spawnPos = playerPos + playerDirection*spawnDistance;
Debug.Log(spawnPos);
Instantiate(tree, spawnPos, Quaternion.identity);
}
}
}
This works fine. But the issue is that the trees are being created a little above the ground instead of directly on top of the ground. Can someone please help me fix this? thanks!
Comment
Your answer