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 eddiesd89 · Feb 25, 2019 at 05:10 PM · rigidbody.addforcetransform.rotate

transform.Rotate doesn't work inside Start() method

I'm trying to write a script that will launch projectiles using RigidBody.AddRelativeForce. I also want to have random rotation around vertical axis (set before applying force), so that each projectile can have slightly different trajectory. Simplified version of my code looks like this

 using UnityEngine;
 public class BalisticProjectile : MonoBehaviour
 {
   public RigidBody rb;
   public float speed = 400.0f;
   public float yOffset = 15.0f;
 
   void Start()
   {
     Random.seed = System.DateTime.Now.Millisecond;
     transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
     rb.AddRelativeForce(Vector3.forward * speed);
   }
 }

Basically, upon instantiation (which is handled by another script), projectile would fly forward until it falls to the ground. Problem is, each projectile launched this way have exactly the same trajectory. It seems like transform.Rotate has no effect on the object. I know there are other (probably better) ways to randomize the trajectory, but I want to know why is it not working in this particular case. Are statements inside Start() method executed in correct order or is AddRelativeForce somehow executed before transform.Rotate?

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 Tsaras · Feb 25, 2019 at 05:16 PM 1
Share

Could be an issue with the coordinate systems. Try substituting with AddForce() and transform.forward ins$$anonymous$$d.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Tsaras · Feb 25, 2019 at 05:24 PM

Vector3.forward is a constant vector pointing to (0,0,1) the world space system. So you are in effect sending the bullets in the same direction. Use transform.forward to get the corresponding vector in local space.

edit: correction, transform.forward is still in world space.

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 eddiesd89 · Feb 25, 2019 at 06:18 PM 0
Share

It worked. I substituted line 12 with

 rb.AddForce(transform.forward * speed);

Projectiles are flying in the direction the weapon is pointing to, and they have random Y rotation. The answer given by @fhilleri is also working fine.

avatar image
1

Answer by fhilleri · Feb 25, 2019 at 05:33 PM

Try with that :

 void Start()
    {
      Random.seed = System.DateTime.Now.Millisecond;
      transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
      rb.AddForce(transform.TransformDirection(Vector3.forward) * speed);
    }
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 eddiesd89 · Feb 25, 2019 at 07:19 PM

Both answers working correctly. What bothers me is that my script also fired the missiles in the direction the weapon was pointing to, but without randomly changing their trajectory due to difference in Y rotation. I fired several projectiles into the air, paused the frame and checked their rotation in the scene view. Each had different Y rotation, meaning that transform.Rotate was indeed applied, but somehow it didn't have effect on how AddRelativeForce was throwing them. Strangely, if I modify the code this way

 using UnityEngine;
 public class BalisticProjectile : MonoBehaviour
 {
   public RigidBody rb;
   public float speed = 400.0f;
   public float yOffset = 15.0f;
   private bool isFired = false;
 
   void Start()
   {
     Random.seed = System.DateTime.Now.Millisecond;
     transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
   }
 
   void Update()
   {
     if(!isFired)
     {
       rb.AddRelativeForce(Vector3.forward * speed);
       isFired = true;
     }
   }
 }

executing transform.Rotate in the Start() function, and use Update() function to AddRelativeForce only once, the code seem to perform exactly like the ones given by @Tsaras and @fhilleri

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

101 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recoil function: smoothly rotation 1 Answer

Excluding Y axis rotation from a vector3 variable? 0 Answers

Stopping transform.Rotate on an exact rotation value 1 Answer

Is it ok to mix transform.Translate and rigidbody.AddForce 0 Answers

Ball moving much faster forward when there is horizontal input 0 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