Question by
Azranoth · Nov 11, 2016 at 08:24 PM ·
instantiatepositionobjectkeypress
Problem when instanciating objects on keypress
Good evening!
I'm actually editing my first project on Unity3D, and I'm facing some issue with a method.
I'm trying to instantiate traps on keypress where the player is looking at. That works, however, even once it's created the trap is still linked to the player position and moves as the player does.
Here's the code:
public class Trap : MonoBehaviour {
public int _nbrHuntingTraps = 1;
public GameObject _trap;
public GameObject _cameraPivot;
public GameObject _inGameUI;
public GameObject _spawnedTrap;
// Update is called once per frame
void Update () {
PlaceTrap ();
}
/**
* Method: PlaceTrap()
* Param: None
* Desc: Allow the player to place a trap where he's looking
* Return: None
**/
public void PlaceTrap(){
if ((_nbrHuntingTraps != 0) && (Input.GetButtonDown ("mouse 1"))) {
/*Fetch player's position in a Vector3*/
Vector3 cameraTransformPosition = new Vector3(_cameraPivot.transform.position.x,
_cameraPivot.transform.position.y,
_cameraPivot.transform.position.z);
cameraTransformPosition += (_cameraPivot.transform.forward * 0.5f);
Vector3 ObjectSpawnPosition = new Vector3 (cameraTransformPosition.x,
cameraTransformPosition.y,
cameraTransformPosition.z);
/*Creating a trap in front of the player*/
Instantiate (_trap, ObjectSpawnPosition, Quaternion.identity);
/* Updating number of remaining traps in Trap.cs & InGameUI.cs*/
_nbrHuntingTraps--;
_inGameUI.SendMessage ("GetNumberOfTraps", _nbrHuntingTraps);
}
}
}
Do you know what could be the cause?
Thank you!
Comment