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
3
Question by e-bonneville · Mar 17, 2010 at 10:44 PM · rotationaifollowturrettank

Lock rotation of object

How would I lock the rotation of an object to 1 axis? I've got a turret on a tank that follows the player. Unfortunately, it'll follow so strictly that if the player gets too close to the tank, the turret will go through the tank in order to continue following the player. I don't like that. How would I fix it?

Any help greatly appreciated - Frustrated developer

EDIT: Here's the script:

var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform;

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; }

function Update () { if (target == null) return;

 if (!CanSeeTarget ())
     return;

 // Rotate towards target    
 var targetPoint = target.position;
 var targetRotation = Quaternion.LookRotation (targetPoint -     transform.position, Vector3.up);

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

// If we are almost rotated towards target - fire one clip of ammo var forward = transform.TransformDirection(Vector3.forward); var targetDir = target.position - transform.position; if (Vector3.Angle(forward, targetDir) < shootAngleDistance) SendMessage("Fire"); }

 function CanSeeTarget () : boolean

{ if (Vector3.Distance(transform.position, target.position) > attackRange) return false;

var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;

return false; }

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 Michael La Voie · Mar 17, 2010 at 10:45 PM 0
Share

Could you post your follow script please?

avatar image e-bonneville · Mar 17, 2010 at 11:12 PM 0
Share

Yup. I edited it in.

avatar image cregox · Mar 22, 2010 at 04:00 PM 0
Share

@elbon96 I don't get it. Please read my comment below: http://answers.unity3d.com/questions/5980/lock-rotation-of-object/5991#5991

4 Replies

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

Answer by qJake · Mar 17, 2010 at 11:03 PM

Sometimes what I do is just this:

float lockPos = 0;

void Update() { transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, lockPos, lockPos); }

Whatever you set the "lockPos" to, is what the axes will lock at.

And wherever you feed back its own rotation (transform.rotation.eulerAngles.x, for instance), is a "free angle", and will not be locked.

The same thing can be done to position, as well, to lock an object to one or two axes.

A Configurable Joint will also work, but may not be the correct solution depending on your game. I've used both ways in various objects, and each has its place. It just depends upon your particular code/object.

Edit: Sorry, you're using JavaScript... this is what it would be, I believe (I don't know Javascript all that well, so it may not be exact):

var lockPos = 0;

function Update() { transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, lockPos, lockPos); }

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 cregox · Mar 18, 2010 at 12:07 AM 0
Share

That's good stuff. Too bad you followed my lead on the answer - it was going to the wrong direction. :P

avatar image
5
Best Answer

Answer by Eric5h5 · Mar 18, 2010 at 12:11 AM

In the script you posted, after the "var targetPoint" line, insert this:

    targetPoint.y = transform.position.y;

Then it will only rotate around the Y axis, since the target point will always be on the same plane as the turret.

Comment
Add comment · Show 2 · 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 cregox · Mar 22, 2010 at 03:59 PM 0
Share

@elbon96 I don't get it. You said colliders didn't solve your question after having accepted this. Was the question really just about restricting the rotation Axis, or was it a miss-question actually asking about restricting the movement, and "locking" it not to the axis, but to make a semi-sphere and just not colliding with the tank?

avatar image e-bonneville · Mar 22, 2010 at 06:51 PM 0
Share

Yeah, the question was kind of confusing. I was trying to restrict the rotation of the turret to the Y axis (I thing vertical is Y) so it wouldn't rotate in x or z at all.

avatar image
2

Answer by cregox · Mar 17, 2010 at 10:58 PM

third attempt to answer:

Well, like I meant on the comment: d'oh. Colliders will be overwritten by any script, and you already showed yours were doing just that. I should have noted that before.

So you may need to go with either OnCollisionStay or OnTriggerStay. The second is not set every frame, so you'd have to try it and see if it behaviors is accepted. I'll give an instance on it because it's simpler! :P

By the way, if you still haven't done so, add a rigidbody as a requirement for the collider to actually collide (see Static vs Rigidbody Colliders on the link).

Then you should add a variable to check if the turret is coliding with the tank and verify it using OnTriggerStay and OnTriggerExit.

It could be something like this:

private var lastTransform : Transform; function OnTriggerStay (other : Collider) { if (other.tag = "turret") { transform.rotation = lastTransform; } }

// This goes on the Update function, right before the transform.rotation lastTransform = transform.rotation; transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

That must be enough. But if for whatever reason it isn't, you'll also need to add a boolean and check for that var before actually rotating the turret. Something like this:

private var canRotate = true; private var lastTransform : Transform; function OnTriggerStay (other : Collider) { OnTriggerExit(); if (other.tag = "turret") { canRotate = false; transform.rotation = lastTransform; } } function OnTriggerExit () {canRotate = true;}

// This goes on the Update function, right before the transform.rotation if (canRotate) { lastTransform = transform.rotation; transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); }

Note that this won't work just with that alone because the turret will just stop once it collides and nothing will make it exit!

second attempt:

Ok, so C.Joint didn't work for you... That became clear by reading your code now. Unless you're making a 2D game, it's very unlikely C.Joint would be useful in your case. The code was also actually not needed there, but it helped to elucidate the question (which was almost enough in itself).

I think what you need there is adding colliders to the tank and the turret, where the turret goes through to avoid that from happening. That's the whole point of colliders. Learn more about them, it should be enough to make the turret stop going through.

first attempt:

Well, to lock rotation to 1 axis you can simply add a Configurable Joint to it, and lock the other 2 "Angular Motion".

As for the rest, like Michael said, we need more details to help you.


Anyway, since we're here, I'd like to add: always avoid using "Find" as much as possible because it's slow and resource hungry. It would probably not make much difference in this case, but I still rather give you the example of a better usage. It's not an optimal solution:

function Start () {
    findPlayer = GameObject.FindWithTag("Player").transform;
    if (target == null && findPlayer)
        target = findPlayer;
}
Comment
Add comment · Show 4 · 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 e-bonneville · Mar 17, 2010 at 11:27 PM 0
Share

Hmm... That doesn't seem to be working for me. It works without the C.Joint, but when I add it, the turret goes crazy. It's rotation is completely messed up. It even starts moving towards me. Any ideas?

avatar image cregox · Mar 18, 2010 at 12:05 AM 0
Share

@elbon96 Yeah,, couple ideas. I just edited the answer. ;)

avatar image e-bonneville · Mar 20, 2010 at 02:26 PM 0
Share

@Cawas Bit late for a comment, but even with colliders, it still goes through.

avatar image cregox · Mar 20, 2010 at 08:48 PM 0
Share

@elbon96 well, then make the colliders become triggers and threat it on the code, probably changing just the Update function... I'll edit this as soon as I can to show you how.

avatar image
0

Answer by dhurstdev · May 07, 2016 at 01:03 AM

Everyone... Much similar than worrying about locking an Axis... I have wasted too much time on this, trying to calculate the angle and solve the BS... Apparently Quaternion.LookRotation, does this without having to work about correcting angles, or flipping! Amazing! http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html Hope this helps: private void SetRotationFromWorldPoint(Transform trans, Vector3 worldPoint) { Quaternion currentRotation = trans.rotation; currentRotation = Quaternion.LookRotation(worldPoint); trans.rotation = currentRotation; }

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

2 People are following this question.

avatar image avatar image

Related Questions

question about turret on tank 1 Answer

rotating a tank turret while the tank is rotating 3 Answers

Rotate on Z axis 2 Answers

Rotating turret floats off tank in a sideways circle. Cannon floats out of turret when rotating up. 0 Answers

Turret AI script off rotation 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