- Home /
Question by
nzmichael118 · Sep 06, 2017 at 01:56 AM ·
c#prefabunity5fps controllerplacement
Create a object in front of the player
I have a prefab named plant. and I want to make it place in front of the player when the player Right clicks In what ever direction the player looks just in front of them while INGAME!
Comment
Answer by JedBeryll · Sep 06, 2017 at 06:58 AM
private const float placingDistance = 5f; //item distance from the player when placed
public GameObject plant; //assigned in inspector
private void Update() {
if (Input.GetMouseButtonDown(1)) {
//assuming this script is attached to the player object
Vector3 position = Vector3.MoveTowards(transform.position, transform.position + transform.forward, placingDistance);
Instantiate<GameObject>(plant, position, Quaternion.identity);
}
}
Note that it might have errors, I haven't checked.