- Home /
Place object in place of ghost.
Hello i am having a little trouble getting my crafting system working correctly, At the moment i can place an object but im having 2 problems,
1) The Ghost object is locked to camera view, it should be on terrain and level "upright" 2) Once i press B the object spawns in a fixed direction from the player, It should place object in place of where ghost was.
Sorry if i did not explain very well, Any suggestions would be great. Thanks.
using UnityEngine;
using System.Collections;
public class CampfireBuild : MonoBehaviour {
public Transform campfirePrefab;
public GameObject player;
private bool canBuild = true;
void Start (){
renderer.material.color = Color.green;
//renderer.material.color.a = 0.5f;
Color color = renderer.material.color;
color.a = 0.5f;
renderer.material.color = color;
}
void OnTriggerEnter ( Collider col ){
if(col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree")
{
renderer.material.color = Color.red;
//renderer.material.color.a = 0.5f;
Color color = renderer.material.color;
color.a = 0.5f;
renderer.material.color = color;
canBuild = false;
}
}
void OnTriggerExit ( Collider col ){
if(col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree")
{
renderer.material.color = Color.green;
//renderer.material.color.a = 0.5f;
Color color = renderer.material.color;
color.a = 0.5f;
renderer.material.color = color;
canBuild = true;
}
}
void Update (){
if(Input.GetKeyDown("b") && canBuild == true)
{
Instantiate(campfirePrefab, player.transform.position + new Vector3(0, 0, 5), Quaternion.identity);
player.GetComponent<Crafting>().campFire.SetActive(false);
}
}
}
@BritishGuy, I don't understand what is ghost object. and where you attached this script as component?
@yashpal The object i want to build is a floating green box for now that turns red if it hits a collider "Ghosted" until i press B to build it then it will spawn the actual object, The script is attached to the camp fire object
@BritishGuy Instantiate(campfirePrefab, GhostPosition.transform.position, Quaternion.identity); I don't understand your first question you can put your camera as you wanted how it Ghost object block your camera?
@yashpal Thanks i used something very similar.
Instantiate(campfirePrefab, transform.position, transform.rotation);
ok for the Ghost and camera, You know how in RTS games when you want to build a structure a ghost version of the structure will be stuck to the mouse, clicking to place the structure, Isnt there a way to do the same in first person but from a raycast, so where ever i look the ghost object will be where ever i look on the terrain,
Sorry if its sounding complicated, im trying to explain my issue as simple as possible. Thanks.
ok i have almost got it, Just cant figure out a way to make the objects always face the camera when i move around.
void Update (){
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 500))
{
//Debug.Log(hit.collider.gameObject.name);
//Debug.DrawLine (ray.origin, hit.point, Color.green);
campFire.rigidbody.$$anonymous$$ovePosition(hit.point);
}
}
Your answer
Follow this Question
Related Questions
script dosnt creat object where i want it to 1 Answer
Spawn object in cube 2 Answers
'Pre-create object VS Create when needed' Performance difference 1 Answer
Key press hides object and spawns another 2 Answers
Spawn an object? 2 Answers