Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xigbar1998 · Oct 10, 2017 at 02:19 AM · instantiatedirection

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);                         
          }        
 }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raulssorban · Oct 10, 2017 at 01:38 PM 0
Share

Here you go.

 rigidbody.AddForce( player.transform.forward * force, Force$$anonymous$$ode.Impulse );

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zartl10 · Jan 18 at 01:50 PM 0
Share

Thank you.

Indeed, I was searching for this on Jan 18th, 2022.

Blessings upon you ser.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with the look direction after Instanciate/Rotate 0 Answers

Instantiate Object in direction from another 1 Answer

Rotation and Velocity of bullets. 1 Answer

transform.position innacurate? [Unity 2D] 1 Answer

Instantiated objects always facing the center 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges