- Home /
How do you Place/Spawn a GameObject or Prefab in front of Player
Hello, I am making a game where Player can pick up "walls" just like picking up coins or points. I am trying to have it so if the player has say "2 walls" than he can hit the '1' or 'e' keys and a wall GameObject or Prefab will appear in front of the character. I need help with the code I don't know where to start or anything. Here is the code for my Wall pickup (This code is on the Player)
var PlayerScore : int;
var ScoreText = "Walls: 0";
var MaxPoints : int;
var gText : GUIText;
function OnTriggerEnter(other : Collider){
if(other.tag == "Point"){
PlayerScore += 1;
ScoreText = "Walls: " + PlayerScore;
Destroy(other.gameObject);
}
}
function Update(){
if(PlayerScore > MaxPoints){
PlayerScore = 0;
print("MaxReached");
ScoreText = "Walls: 0";
}
gText.text = ScoreText;
}
Please help me with the code for placing the GameObject in front of the Player. Do I need to make a new JavaScript or addon to this Wall one. If it is a new Javascript where do I add it to? Thank you for your time, I would appreciate any help I can get.
Answer by clunk47 · Dec 16, 2012 at 10:58 PM
Here. Use this as a separate script just for testing. Attach this UnityScript to your player. Drag a prefab or other object onto the slot for (Prefab Object) in the inspector. When you press 'E', it will instantiate that object 3 units(default) in front of the player. You can take this and tweak it to your needs, and add what you need from it to your script. You can enable and disable the boolean "CanPlaceObject" via your player's "Wall Score". Something like
if(PlayerScore > 0)
canPlaceObject = true;
else if(PlayerScore <= 0)
canPlaceObject = false;
That's just an example. Here is my script.
public var prefabObject : GameObject;
var canPlaceObject : boolean = true;
private var offset : Vector3;
private var fwd : Vector3;
private var clone : GameObject;
var unit : float = 3.0f;
function Update()
{
fwd = transform.TransformDirection(Vector3.forward) * unit;
offset = transform.position + fwd;
if(Input.GetKeyDown(KeyCode.E) && canPlaceObject)
{
clone = Instantiate(prefabObject, offset, transform.rotation);
}
}
Very helpful Script but I am still confused on the if statement. Would I put it on this new script or my Wall Pickup script?
So if you're wanting to take pieces from this script, yes, you would copy from this script and paste into the script you had originally attached to your player.
Answer by strachpr01 · Dec 16, 2012 at 10:14 PM
you can add the following to this script
private var playerPos:Vector3;
private var spawnPos:Vector3;
public var player: GameObject;
public var objectToInstantiate: GameObject;
function DoInstatiate(){
playerPos=player.transform.position;
spawnPos= Vector3(playerPos.x+10, playerPos.y, playerPos.z); ///change as needed
Instantiate (objectToInstantiate, spawnPos, Quaternion.identity);
}
then just call this function when you wish to create the object
What do you mean by call this function. How do I call that function? Put it in another function that activates when 'e' is pressed? What code do I need to put so that the above code is called when 'e' is pressed?
Your answer
Follow this Question
Related Questions
Scripts wont work if deactivated then activated? 0 Answers
Spawn game object in random position on screen 1 Answer
Spawn random amount apart of each other 1 Answer
Slender pages collection does not activate gameobject I want 0 Answers
How to instantiate multiple Prefabs to multiple Childs randomly ? 2 Answers