- Home /
Need help with building system
Hello guys and girls. I am currently making a survival game, just for the challange and i just started working on the building system, but i just ran into a problem i just can't seem to find information on this subject by google searching(might be searching the wrong words/terms).
Okay so is the code i have to far:
var RayHit : RaycastHit;
var RayPL : int = 10; //RayPlaceLenght
var InventoryInfo : InventoryText;
var Foundation : GameObject;
function Start ()
{
InventoryInfo = gameObject.Find("Player").GetComponent(InventoryText);
}
function Update ()
{
var RayPlacing1 : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0)); //This is the center of the screen, the point where i want to place the object foundation.
if(InventoryInfo.PlacingFDT == true)
{
if((Physics.Raycast(RayPlacing1, RayHit, RayPL))) //Doing the raycast from the middle of the screen.
{
print("RaycastHit " + RayHit.collider.gameObject.name); //Just checking the name of the object i hit.
if(RayHit.collider.gameObject.tag == "Terrain") //if i hit the terrain that have the tag "Terrain", do the following.
{
print("Insert prebuild foundation code here"); //This is the line i need help to do, i want to instantiate a single gameobject that is placed at the RayHit point, so i dont make multiple objects when i look around.
if(Input.GetKeyDown(KeyCode.Mouse0)) //Press the mouse to build acutal object
{
Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.
}
}
}
}
}
So the thing i need help with is, how i place a single gameobject and then have it placed at the point where my RaycastHit hits the terrain. I tried with instantiate and it didn't work.
Thanks in advance.
Renaxi
"... it didn't work."
We need more info. What are you seeing? Is the object created? Is it not in the right place? How does it differ from what you expected?
Look in the editor window while it's running, select the instantiated object in the hierarchy and press F to find it.
Sorry i should have said something else than "it didn't work". What i meant by it didn't work, was that the object (Foundation) is created on every frame if the raycast hits the terrain, and this doesn't work for me.
What i want the script to do, is to create a single object, that changes position according to where the RaycastHit, hits the terrain, not to create multiple objects everytime the RaycastHit hits the terrain.
Answer by Bluntweapon · Sep 14, 2014 at 05:27 PM
I'm assuming you mean you want to move the object after you've created it once.
So instead of
Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.
You need to hold a reference to the object you have created.
var objectIveMade : GameObject;
function Update(){
...
if(Input.GetKeyDown(KeyCode.Mouse0)) //Press the mouse to build acutal object
{
if( objectIveMade == null ){
objectIveMade = Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.
} else {
objectIveMade.transform.position = RayHit.point;
}
}
}
Your answer
Follow this Question
Related Questions
[SOLVED] Only instantiating once 1 Answer
How to get info of object within certain range?(Javascript) 1 Answer
How to damage gameobject via raycast 1 Answer
Detect Object that are hit by a ray 3 Answers
Creating a teleportation gun 1 Answer