- Home /
create a gameobject in front of fpsplayer
Whenever I create the object it is never truly in front of the player but randomly off to the side. I been toying with it, but cant find the problem. I think having transfrom.position.x is wrong because it doesnt take in the consideration where the x vector (forward) is pointing. I don't know how to get the object in front of the camera....
static var canThrow : boolean = false; var throwSound : AudioClip; var turret : GameObject; var wall : GameObject;
var blank : GameObject; var throwForce : float;
function Update () {
if(Input.GetKeyDown("y") ){ audio.PlayOneShot(throwSound);
var place : GameObject = Instantiate(blank, transform.position, transform.rotation);
place.transform.position.x = transform.position.x + 20.0;
var newthing : GameObject = Instantiate(wall, place.transform.position, place.transform.rotation);
AstarPath.active.SetNodes (false,newthing.collider.bounds,true);
Destroy(place);
}
}
@script RequireComponent(AudioSource)
Answer by Neo 2 · Jul 31, 2010 at 09:25 AM
maybe you could use a code like this (C#)
// Distance from your player float distance = 4;
// Transforms a forward position relative to your player into the world space
Vector3 throwPos = Transform.TransformPoint( Vector3.forward * distance );
// Instantate the object on the position
GameObject place = Instantiate ( blank, throwPos, Quaternion.identity );
Answer by Eric5h5 · Jul 31, 2010 at 02:46 AM
The x vector is to the right, not forward, and it's in world space. Instantiate the object using
transform.position + transform.forward * 20.0
and remove the transform.position.x
code. Also there doesn't seem to be any point in the first Instantiated object since you just destroy it immediately anyway and it's not giving you any information you don't already have, so you can take that out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070134im_/https://answers.unity.com/themes/thub/images/avi.jpg)