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
1
Question by RetepTrun · Jun 01, 2011 at 02:25 AM · rigidbodyrotateaxisturretconstrain

Constrain Turret Rotation while child of Rigidbody

Scenario: A tank drives along an incline steep enough that the tank is nearly about to barrel roll into the ditch. The commander sees an enemy. The gunner first turns the turret to face the target as best he can but this is made difficult by the slope, he then compensates using the elevation of the gun to strike the enemy.

I am trying to create an AI tank with a turret mounted on top. But this is more difficult than just having a car and sticking the FPS tutorial's sentry gun on the roof

The sentry gun operates like a ball and socket joint, however I am looking for more the combination of swivels like on a camera's tripod, a swivel for full 360deg rotation about one axis and another hinge with 30deg of freedom up or down.

There is more to this than just using "turret.position-target.position"..."quaternion".. "Lookat" and then setting one of Lookat's variables back to 0. This results in ridiculus looking behaviour, with the turret staying level with the world while the rest of the vehicle turns, climbs hill, falls off cliff, rolls into the ditch.

The desired effect is that the turret will be constrained relative to its parent rigidbody while still trying its best to aim at a target.

I dont care how this is achieved, if by workarounds or something clever. I dont mind if its not smoothed or lerped or slerped I would work on that later.

Here is some code I am using, byond the first /* it is all disabled stuff which I gathered from forums/answers/scriptRefrence because I thought it looked promising but didn't work. Though I didn't want to throw any away because it took so long to gather and might still be usefull.

^^^^MY FIRST UNITY ANSWERS QUESTION :-)^^^^^ (btw I cant seems to attach an image it just says nothing chosen after I click open)

var thisTransform : Transform; var target : Transform; var a : boolean;

 function Start() {
   thisTransform = transform;
 }
 
 function Update() {    
     if(a)        thisTransform.LookAt(Vector3(target.position.x,target.position.y,target.position.z));
     else        thisTransform.LookAt(Vector3(target.position.x,thisTransform.position.y,target.position.z));
 }
 

[1]: http://

Comment
Add comment · Show 2
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 Chris D · Jun 02, 2011 at 10:28 PM 0
Share

Just a note on the code that you've posted - most people will see that wall-o-text and run the other direction. If you're not using most of it, don't post that stuff.

avatar image RetepTrun · Jun 03, 2011 at 12:50 PM 0
Share

Well sounds worth a try to get more answers, it might scare some away, but I suspect that if they are intimidated by some commented out code they don't know any more than me anyways :)

2 Replies

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

Answer by RetepTrun · Jun 05, 2011 at 02:22 AM

Aha I have found the answer here. This script can be easily modified and constrained.

http://answers.unity3d.com/questions/125748/leftright-updwn-of-me-detection.html#answer-126251

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 Joshua · Jun 05, 2011 at 02:29 AM 0
Share

Yeah, you can definitely use this to have one part rotate left/right and one part up/down (like a turret).

avatar image
1

Answer by Chris D · Jun 02, 2011 at 10:51 PM

From your description of how you want it to work, it sounds like you'll probably want to use a bunch of simple rotations with appropriately parented objects.

What I'm imagining is the following:

Tank Body -> Turret -> Gun barrel

In your tank's hierarchy, meaning that the tank is the ultimate parent. This way, you'll move your tank and the turret and gun barrel will move properly in relation to your main body. Handling the aiming will come down to rotating the turret (within whatever constraints you have) and then rotating the gun barrel (within your constraints) with the appropriate inputs. Transform.RotateAround() is a good function for this kind of thing.

This will take care of the tank if it's to be controlled by the player but if you're trying to create AI, you'll have to come up with some sort of best-solution-target-acquiring code to make it work.

Hope that's clear/it helps...

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 RetepTrun · Jun 03, 2011 at 01:46 PM 0
Share

Yup thats my heirarchy, and I'll look into RotateAround() to see if it could be usefull.

Your thinking is the same as $$anonymous$$e was when I started and it works just fine with transform.rotate but once I try using lookat, quaternion, and more fancy stuff, these are difficult to constrain and seem to ignor the rotation of a heirarchy parent.

Simply choosing a good heirarchy means that turret moves with the vehicle but in the funny way I was trying to describe. The tricky part that I can't get is constraining things it to parent-related limits rather than a world-related limits. Like I have successfully kept the turret rotating 360 about 1 axis only, but this unfortunately this has also meant that it is permanently level with the world axis, so even if the tank body is turned upside down, the turret is still rightside up.

To limit the rotation relative the parent while using transform.lookat probably needs some vector3 math so I think maybe I should make my own targeting from scratch so I can stick with the easy transform.rotate . I have been thinking of just using something that rotates left for 2 deg/frame if the target is to the left, otherwise go right 2 deg/frame, likewise for the barrel go clockwise about x if target is up, otherwise counterclockwise. It works fine for the lego NXT set making a car follow a line. The difficulty is how to detect if the enemy is left/right, up/down of me. Any ideas anyone?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to rotate a rigidbody without its attached anchor rigid body flying off? 0 Answers

Rotate rigidbody using MoveRotation() around the global y axis 2 Answers

Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers

how can I rotate object reading from excel data & time steps 3 Answers

Rotating a rigid body to face left or right smoothly 4 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