Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mrlafle · Dec 03, 2016 at 12:00 AM · rigidbodyinstantiatevelocityrigidbody.addforcetransform.forward

Trouble with rb.Addforce(transform.foreward)

I know this is a noob problem but I've been searching the web for a solution for hours and nothing I've tried has worked. I'm trying to launch an instance of a projectile on the local z axis as it is instantiated from the cannon's script.

Projectile Script:

 #pragma strict
 
 var rb:Rigidbody;
 var ballInitialVelocity:int;
 
 function Start () {
     rb = GetComponent(Rigidbody);
     rb.AddRelativeForce(transform.forward * ballInitialVelocity);
 }

And this is the line of code that instantiates it:

 Instantiate(redBall, transform.position + 2 * transform.forward, transform.rotation);

It looks pretty good to me, but the instance has no velocity when it instantiates. Can anyone figure out what I'm doing wrong?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 03, 2016 at 12:47 AM

This makes no sense:

 rb.AddRelativeForce(transform.forward * ballInitialVelocity);

either use

 rb.AddForce(transform.forward * ballInitialVelocity);

or

 rb.AddRelativeForce(Vector3.forward * ballInitialVelocity);

Both will do exactly the same. Also it doesn't make much sense to declare ballInitialVelocity as "int". Change it into a float and make sure you actually assigned a value in your prefab

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 mrlafle · Dec 03, 2016 at 03:49 PM 0
Share

Thanks Bunny83 it worked!

avatar image
0

Answer by Rainwind · Dec 04, 2016 at 08:30 AM

I am still pretty new to Unity as well, and I came across your post looking to learn a bit more about being able to move rigid bodies using RigidBody.AddForce instead of transform.position (it is messing with my collisions). One thing that might help you is I know when using transform.position I must also use Time.delta in order to get it to move properly. It could be another way for you to get your bullets to move instead of rb.addforce too. Check it out:

example

float MovSpd = 5.0f;

transform.position += transform.forward x Time.deltaTime x MovSpd;

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 rexxthunder · Dec 04, 2016 at 08:30 AM

Don't use "relative" force in this context, tranform.forword is a vector in world, not local space. If you want something to shoot straight out of the weapon using AddRelativeForce, use this parameter, "Vector3.forward * ballInitialVelocity" instead. The function will transform force this into the local space of the rigidbody.

the reason your projectile is not moving is because the force is not a persistent property of the RigidBody, it's an impulse speed that is applied on that frame only.

In order for the force to be applied on a constant basis, you have to put AddForce in an update loop.

void Update() { rb.AddForce(transform.forward * forceamount); }

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

83 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

Related Questions

Throwing knife doesn't throw correctly 0 Answers

Resume movement of instanced object relative to the original after instantiation. 2 Answers

Instantiate and add Rigidbody force 3 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Throwing knife goes straight 1 Answer


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