Object Appear When Hit Terrain And Then Place it
i'm really new about this. i hope you know about Game Like Unturned (Survival Game) and i love it. my question is, how i can make the object appear when mouse hit the terrain and keep it height on the terrain even up when hit the hills?
i hear we can use raycast to make object appear using set active = true. and when i try it, it successed. but the object's rotation wrong, and sometimes it pass throught terrain when i move the mouse down.
i love if these object when hit terrain will appear and go near me when i move the mouse down. so when i place it, it still same rotation as terrain.
i'm also try OnTriggerEnter(). and set IsTrigger. well. you know and i know it will pass througth terrain. :D any help will be appreciated. and hope get new script which has raycast, if you dont mind. thanks
here's my Script. for example, my object is campFire. and this without Raycast. so when it pass the terrain, the object changes red and that mean we cant put the campFire.
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class CampFireBuild : MonoBehaviour {
public Transform campFirePrefab;
public GameObject campFireBP;
private GameObject player;
private Crafting playerScript;
private Transform target;
private CharacterController karakter;
private Renderer rend;
private Color myAlp;
private float speed = 1;
private float maksimalJarak = 2;
private bool canBuild = true;
// Use this for initialization
void Start () {
player = GameObject.Find ("FPSController");
target = player.transform.position;
playerScript = GameObject.Find ("FPSController").GetComponent<Crafting> ();
campFirePrefab.GetComponent<Rigidbody> ();
rend = GetComponent<Renderer> ();
rend.enabled = true;
rend.material.color = Color.green;
myAlp = rend.material.color;
myAlp.a = 0.5f;
rend.material.color = myAlp;
//rend.material.color.a = 0.5f;
}
// Update is called once per frame
void OnTriggerEnter(Collider col) {
if (col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree") {
rend.material.color = Color.red;
myAlp = rend.material.color;
myAlp.a = 0.5f;
rend.material.color = myAlp;
//rend.material.color.a = 0.5f;
canBuild = false;
}
}
void OnTriggerExit(Collider col) {
if (col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree") {
rend.material.color = Color.green;
myAlp = rend.material.color;
myAlp.a = 0.5f;
rend.material.color = myAlp;
//rend.material.color.a = 0.5f;
canBuild = true;
}
}
void Update () {
Vector3 temp = Input.mousePosition;
temp.z = 10f;
this.transform.position = Camera.main.ScreenToWorldPoint (temp);
if (Input.GetKeyDown ("b") && canBuild == true) {
Instantiate (campFireBP, campFirePrefab.transform.position, Quaternion.identity);
playerScript.campFire.SetActive (false);
}
}
}