Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 sevensixtytwo · Sep 09, 2014 at 01:19 AM · rotationraycasttransformquaternionrotatetowards

Child transforms screwed up by parent's rotations

The transforms of children get changed whenever I rotate their parents. The problem manifests itself in several ways:

EXHIBIT A: I have a turret object that rotates on the horizontal axis using Quaternion.RotateTowards. I have a childed camera parented to the turret at zero localRotation but set a bit higher (y+1). The camera is controlled by MouseLook and I Raycast from the camera using its transform.forward in order to set targets for the turret.

When the turret is at rest (pointing forward) I can raycast accurately, but when it is rotated more than +-15 degrees, the Raycasts go off target. They start off in the right place, but shoot either higher (+rotation) or lower (-rotation) than the center of the screen.

EXHIBIT B: I have a muzzle childed and placed a certain distance forward of the turret. It has zero localRotation. When firing, a projectile is instantiated, using the muzzle's transform.forward.

Again, when the turret is at rest, the projectile speeds straight from the muzzle. But when the turret is rotated, the projectile goes off target, either flying into the sky or slamming into the ground in front of the turret.

EXHIBIT C: I have a particle system childed to the muzzle that is supposed to create a jet of smoke particles forward every time I fire the gun. I have noticed that it sprays the smoke EVERYWHERE, even if the projectile is on target.

alt text

As you can see, there are TWO jets of smoke.

CONCLUSION: I have tried eliminating most other scripts that affect the rotations until only the turret rotates, but same deal. Raycasts and projectiles still go off target. I even tried using Camera.ScreenPointToRay, but same result.

I also guarded the child's transforms by resetting their localPositions and localRotations every frame. No joy.

One way I mitigated this was to not make the muzzle and camera children of the turret and simply set their position every frame. It works to a point, but might end up giving me more problems down the road.

CODES: Here are the codes I use to rotate and RayCast:

 function RotateTurretTo (aimpoint:Vector3) {
 
     var turretRotation = Quaternion.LookRotation(aimpoint-turret.position, Vector3.up);
     turret.rotation = Quaternion.RotateTowards(turret.rotation, turretRotation, turretSpeed);
     }
 
 function Fire() {
 
     if (canFire) {
         var shot = new Instantiate(bullet,muzzle.position,muzzle.rotation);
         shot.rigidbody.velocity += this.rigidbody.velocity;
         canFire = false;
         }
     }

 function CommandTarget () {
 
     var ray = new Ray(viewportCommander.transform.position,viewportCommander.transform.forward);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 10000, targetMask)) {
         if (hit.transform.gameObject.layer == 15) {
             gunner.target = hit.transform;
             }
         if (hit.transform.gameObject.layer == 10) {
             gunner.targetPosition = hit.point;
             }
         }
     }

I've been tearing my hair out for a long while now. If anyone knows how to solve this or even offer insight, it would be greatly appreciated.

ADDENDUM: Added an EXHIBIT C. On further experimentation, I found that lowering the time scale made the problem even worse.

problems.png (434.0 kB)
Comment
Add comment · Show 3
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 Myth · Sep 09, 2014 at 01:23 AM 0
Share

Look into local space rather than world or global space(i forget the exact term) - this will change whether you are rotating relative to the parent or global xyz axies

avatar image sevensixtytwo · Sep 09, 2014 at 01:36 AM 0
Share

Hmm. I actually transferred over from using local space since it didn't work well with my targeting code. I'll see if it makes a difference when I get back to my computer. Cheers.

avatar image sevensixtytwo · Sep 09, 2014 at 01:17 PM 0
Share

No joy. localRotations chuff my rotating code. Badly.

 function RotateTurret(aimpoint:Vector3) {
 
         var targetVector = aimpoint - turret.position;
         var localVector = turret.InverseTransformDirection(targetVector);
         var targetRotation = Quaternion.LookRotation(localVector,Vector3.up);
           turret.localRotation  = Quaternion.RotateTowards(turret.localRotation , targetRotation, 2);
             
     }

It doesn't center on the aimpoint, and spazzes out when it goes from +1 to -1 degrees.

1 Reply

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

Answer by Jan_Julius · Sep 10, 2014 at 01:40 PM

I will get back to this, but have you tried putting an empty game object to where you want them to spawn and then just spawn them there instead?

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 sevensixtytwo · Sep 10, 2014 at 02:10 PM 0
Share

Yes, that is the $$anonymous$$uzzle object indicated in the Fire method. It is parented to the turret.

avatar image Jan_Julius · Sep 10, 2014 at 03:00 PM 0
Share

Try putting a non rotated object in your scene and getting the rotation from that.

avatar image sevensixtytwo · Sep 10, 2014 at 03:10 PM 0
Share

Hrmm, not sure of what you mean by that.

avatar image Jan_Julius · Sep 11, 2014 at 09:43 AM 0
Share

Place an empty game object with (0,0,0) rotations and try getting the rotation from that object just to see what that does.

avatar image sevensixtytwo · Sep 12, 2014 at 12:42 AM 0
Share

Hmm. So I made the muzzle copy an empty gameObject's rotation, like you said, and it still occasionally goes off target.

On closer inspection, the projectile seems to follow the local X rotation of the turret whenever it does screw up. Like something was resetting the muzzle's local rotation to zero every so often. I'll have to look deeper into this.

Show more comments

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

24 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

Related Questions

Unity Rotate Raycast on Quaternion 1 Answer

Align navmeshagent to ramp and rotate normally,Script is aligning to slopes but no longer rotating on other axis 0 Answers

Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers

Object slope rotation issue 0 Answers

How can I multiply the rotation of a mirrored object? 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