Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 amunds · Apr 28, 2013 at 10:34 PM · rotationquaternionaddforce

Rotation of Instantiated object wrong

I have a script that applies force to an Instantiated object with a direction based on the current rotation of the object the script is attached to. My problem is that the instantiated object is always rotated 90 degrees in the wrong direction.

I've tried changing the rotation of the Instantiated object, but then the applied force is is wrong.

 var fwd : Vector3;
 fwd = transform.rotation * (Vector3.forward);
 var bulletClone = Instantiate(bulletPrefab, transform.position + transform.forward*1.7, Quaternion.identity);
 bulletClone.rigidbody.AddForce(fwd * force, ForceMode.Impulse);

How do I get rotate the object without altering the direction force is applied in any way?

EDIT: Just wanted to say that the Instantiated object is a simple cylinder made in Unity. Changing the rotation of the cylinder before making it a prefab has had no effect.

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 rhys_vdw · Apr 29, 2013 at 05:31 AM 0
Share

Note that you can use transform.forward as a shortcut for the second line.

3 Replies

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

Answer by amunds · Apr 29, 2013 at 04:03 AM

I solved it by (perhaps) doing as you intended? Attached the script to the ship. Made a empty gameobject a child of the ship then rotated the empty gameobject 90 degrees on the x-axis. Then in my script I changed AddForce to use .up instead of .forward. Final function ended up like this:

 function shootBullet() {
     var bulletInstance : Rigidbody;
     bulletInstance = Instantiate(bulletPrefab, spawnPoint.position, spawnPoint.rotation);
     bulletInstance.velocity = spawnPoint.parent.gameObject.rigidbody.velocity;
     bulletInstance.AddForce(spawnPoint.up * force, ForceMode.Impulse);
 }
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
1

Answer by whydoidoit · Apr 28, 2013 at 10:36 PM

You need to put it in a parent, rotate it by 90 degrees and then make the parent the prefab.

Also fwd = transform.rotation * Vector3.forward is just transform.forward;

Comment
Add comment · Show 6 · 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 amunds · Apr 28, 2013 at 10:48 PM 0
Share

Thanks for the fast answer, I'm a bit confused about what you are referring to as "it", do you mean the prefab I'm going to instantiate? Currently I just have an empty GameObject prefab as the child of my Ship prefab. The script that handles the instantiate is on the empty GameObject.

avatar image whydoidoit · Apr 28, 2013 at 10:50 PM 0
Share

I mean put your cylinder "bullet" inside an empty game object, rotate the cylinder and then make the empty go the prefab for bullet - then instantiate that prefab.

avatar image amunds · Apr 28, 2013 at 11:03 PM 0
Share

Did that now, but it had no effect. No matter how I rotate it the bullets still come out with the same rotation. $$anonymous$$akes me believe the script is setting a fixed rotation.

avatar image whydoidoit · Apr 28, 2013 at 11:06 PM 0
Share

Well if you've done that something would have to be changing the rotation of the child of your prefab - that is pretty unlikely I would have thought... hmmm

avatar image amunds · Apr 29, 2013 at 12:22 AM 0
Share

Perhaps I'm misunderstanding something, could you please provide a simple example of the structure you'd use?

Show more comments
avatar image
0

Answer by Roixo · Jan 18, 2018 at 05:27 PM

Other simple way to solve this issue without creating empty parent game object:

 GameObject pc = (GameObject) Instantiate (Prefab, position, rotation, transform);
 pc.transform.Rotate (new Vector3(rotationWished.x,rotationWished.y,rotationWished.z));

Where you choose in Vector3 rotationWished which rotation you want.

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

16 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

Related Questions

2D Rotate towards GameObject acting odd. 2 Answers

Roll a ball camera problem? 1 Answer

Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer

Quaternion to Matrix Conversion Error - Camera Script 0 Answers

Rotation approximation issues using Quaternion.LookRotation 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