- Home /
Instantiating an object in front of the player
Basically I'm trying to drop the player's current weapon in front of him but I can't wrap my head around how to do that. I don't have a problem dropping the object on the player and by adding 1 to the localposition.z of the object I'm instantiating, it kind of does what I want. But it doesn't go with the direction i'm facing. So can someone explain how to make this work? Here is my current code.
function Drop () {
var instance : GameObject;
instance = Instantiate(weaponmodel);
instance.transform.localPosition = player.transform.position;
instance.transform.localEulerAngles = player.transform.localEulerAngles;
instance.transform.localPosition.z += 1;
instance.transform.position.y = 0.14;
player.GetComponent("FPSPlayer").hasprimary = false;
}
Answer by Eric5h5 · Aug 08, 2012 at 04:33 AM
localPosition is used with child objects. If an object isn't the child of another, then localPosition and position are the same thing. Same for localEulerAngles. Anyway you can use transform.forward:
function Drop () {
Instantiate (weaponmodel,
player.transform.position + player.transform.forward + Vector3.up*.14,
player.transform.rotation);
player.GetComponent(FPSPlayer).hasprimary = false;
}
(Also don't use quotes with GetComponent unless necessary, which it almost never is.)
That worked but I changed it a bit,
instance = Instantiate (weaponmodel, player.transform.position + player.transform.forward + (player.transform.right), player.transform.rotation);
I just didn't know what the directions like forward and right did. Thanks
Also it was necessary to use quotes with that get component because it won't let me access the variable otherwise.
Answer by Myth · Aug 08, 2012 at 03:58 AM
var bullit1 = Instantiate(bulletPrefab,transform.Find("spawnPoint1").transform.position,transform.Find("spawnPoint1").transform.rotation); bullit1.rigidbody.AddForce(transform.forward * 6500);
this is a snippit I use to shoot, however, if you want to you can modify it to suit or just add a gameonject (called spawnPoint1) attached to player and use it as it is.
Your answer
Follow this Question
Related Questions
Rotation losing precision 0 Answers
Whats the Difference Between Rotation EulerAngles and Quaternions? 2 Answers
How can I store the individual values of EulerAngles on a Database then re-apply 2 Answers
Bike stay upright based on surface normal 1 Answer
Why is my object's local x rotation not going past 90 degrees? 1 Answer