- Home /
Instantiate a prefab in the direction my character is facing
I want to make my character throw a knife in the direction he is facing. I wrote the code to instantiate the object and another line to add the force to it so it move forward. If i face the way my character is looking and throw, it will go straight, but if I look left or right, it will not throw the new way I am looking. Here is my code, and if anyone can help thank you, I cannot figure it out and have not gotten it to work even after reading other questions:
var Knife: GameObject;
var upspeed: float = -20;
function Update () {
if (Input.GetKeyDown (KeyCode.Mouse0))
{
var clone= Instantiate (Knife, Vector3(this.transform.position.x ,
this.transform.position.y, this.transform.position.z), this.transform.rotation);
clone.GetComponent.<Rigidbody>().AddForce (upspeed,0 ,0 , ForceMode.Impulse);
}
}
Here you go.
rigidbody.AddForce( player.transform.forward * force, Force$$anonymous$$ode.Impulse );
Answer by Pikaseal · Jul 08, 2021 at 05:14 PM
hello, I know this post is 4 years old but just in case there are new people searching for this, the answer to this question is actually more simple than I expected. public GameObject projectilePrefab; void Update() { if (Input.GetMouseButtonDown(0)) { Instantiate(projectilePrefab, transform.position, transform.rotation); } } when the left mouse button is pressed down, the projectile prefab is instantiated in the same position and rotation of the player object this script is attached to.
Thank you.
Indeed, I was searching for this on Jan 18th, 2022.
Blessings upon you ser.
Answer by EyalBira · Oct 10, 2017 at 01:59 PM
When you Instantiate the knife, you provide the proper rotation it seems. The issue is with the Force you are adding.
The rotation does not determine the direction the Knife will move, only how it will be rendered (the object rotation). The movement direction is determine by the Force you add. Current code always adds a Force to the same direction -20 on the X axis. It means that the Knife will always fly at the same direction.
Add the force to the direction you need, which can be calculated by the rotation value like so (pseudo code):
force = rotation.normalized * upspeed
Answer by jeango · Oct 10, 2017 at 02:06 PM
There are some weird things you're doing in that code.
First, if you want the position vector, you don't need to recreate one from scratch.
You can replace
new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z)
with
transform.position
no need to even use "this" (well not in C# anyways, perhaps it's different in javascript)
Oh yeah, since we're talking about javascript, you should really switch over to C#, as Unity will no longer support it in the future.
Second thing: the documentation of AddForce clearly says that x, y and z are in world coordinates and not local coordinates.
so if you want to do this in local coordinates you should do the following:
clone.GetComponent.<Rigidbody>().AddForce(transform.forward * upspeed, ForceMode.Impulse);
transform.forward will return the forward vector of your character (provided this script is indeed placed on your character)
Your answer