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 /
  • Help Room /
avatar image
0
Question by zak666 · Jan 17, 2017 at 07:03 PM · quaternionlookatlock

Use Quaternion.lookat to rotate On One Axis (for turret.)

Hi gang, here is what I'm working on for my Automatic Gun-turret control system. I can select a random target with tag and when it gets out of range it try to find a new target Cool, 'my problem now is when i try to use Quaternion.lookat I cannot seem to get it to lock on Axis

 using UnityEngine;
 using System.Collections;
 
 public class TurretTargeting : MonoBehaviour {
 
     public GameObject Target;
     public GameObject TurretBarrels;
     public GameObject TurretHead;
     public GameObject[] Targets;
     public bool OurTeam;
     public     float distance;
     public int index;
 
     public void LateUpdate(){
 
          distance = Vector3.Distance(Target.transform.position, transform.position);
 
 
         // If the turret is ours........
         if(OurTeam == true){
             if (Target == null){        
                 Targets = GameObject.FindGameObjectsWithTag("TeamTwo");
                 index = Random.Range (0, Targets.Length);
                 Target = Targets[index];
             }
         }
         // If the turret is ours........
 
 
 
         // If the turret is NOT ours........
         if(OurTeam == false){
             if (Target == null){        
                 Targets = GameObject.FindGameObjectsWithTag("TeamOne");
                 index = Random.Range (0, Targets.Length);
                 Target = Targets[index];
             }
         }
 
         // If the turret is NOT ours........
 
 
             // Universal Targeting Range.
             if(distance > 100){
                 Target = null;
             }
 
 
 
 
         // TurretBarrels ROATATE OON VERTICAL AXIS ONLY.
 
         // TurretHead Rotate horizontal axis only.
 
 
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Creeper_Math · Jan 17, 2017 at 07:35 PM

Sidenote...
Your positioning of the Distance formula is weird.. This is what your saying "Find the Distance, then select a random enemy, then test to see if the distance is more..." Don't know if you were trying to do that or not

Back to your Question
Here's a bit of code that would work (With Explanation)
Would be put where you want to rotate the turret

 if (Target != null) {
         // Saves it's current rotation, turns to target, records rotation, and reverts to original rotation immediately. All in one update so it should not show it looking at the target and looking back
     
         Quaternion CurrentRotation = transform.rotation;
         transform.LookAt(Target.transform.position);
         Quaternion GoalRotation = transform.rotation;
         transform.rotation = CurrentRotation;
     
         // Makes Goal Rotation only save it's y value and set the current rotation's y value to the current rotation
         // This can be Changed... The way to do that is to put in "GoalRotation" for all the axis that you want to change, and "CurrentRotation" for those that you don't want changed
 
         GoalRotation = new Quaternion (CurrentRotation.x, GoalRotation.y, CurrentRotation.z, CurrentRotation.w);
         
     
         float Speed = 1.0f;
         transform.rotation = Quaternion.RotateTowards (CurrentRotation, GoalRotation, Speed);
 
     }
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 zak666 · Jan 17, 2017 at 07:55 PM 0
Share
     if (Target != null){        
         TurretBarrels.transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(Target.transform.position - this.transform.position)), Time.deltaTime * TurnSpeed);
     }

     if (Target != null){        
         TurretHead.transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(Target.transform.position - this.transform.position)), Time.deltaTime * TurnSpeed);
     }


this works, (what I'm currently using, but its on every AXIS and looks super strange)

I'm basically trying to do this but lock Gunbarrel to vertical rotation and lock Base to horizontal

avatar image Creeper_Math zak666 · Jan 17, 2017 at 09:30 PM 0
Share
 GoalRotation = new Quaternion (CurrentRotation.x, GoalRotation.y, CurrentRotation.z, CurrentRotation.w);

This is where it takes the rotation of the object, and only the ones that you want to be changed remain to be the goal rotation, while the others switch back to the original rotation.. If you have different objects, then you would have to have a separate copy of the entire script in my answer for each one

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

87 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

Related Questions

Character rotation on ledge hanging 0 Answers

Problem with Quaternion and Euler Angles 0 Answers

Delayed LookAt, but only on one axis 1 Answer

How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers

Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 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