Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 tom.coursow · Apr 11, 2014 at 07:40 PM · rotationquaternionaxistank

Tank urret rotation - Need help.

Hi guys,

for 3 three hours I try to rotate a tank turret now... And I only get it to smoothly look to the target. But the problem is how can I fix the Z axis of the turret after looking/slerping to the target?

Here is an example:

alt text

After this phase of rotation I want that the local rotation of the turret gets to the same level as the tank hull again. Can anyone help me please? I tried so many experiments on my own with this stupid tank :D

Here is my latest code experiment:

 // The Important snippet from the Weapon.cs Script
 //
 // Transform weaponHolder = <turret set via inspector>
 // The parent of the turret is the hull.
 
 Vector3 direction = target.transform.position - transform.position;
 Quaternion toRotation = Quaternion.LookRotation(direction);
 Quaternion newRotation = Quaternion.Slerp(weaponHolder.transform.rotation, toRotation, Time.deltaTime * 1.5);
 weaponHolder.rotation = newRotation;
 weaponHolder.localEulerAngles = new Vector3(weaponHolder.localEulerAngles.x,
                                     weaponHolder.localEulerAngles.y,
                                     weaponHolder.parent.localEulerAngles.z);

tank_rotation_bug.jpg (29.4 kB)
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
Best Answer

Answer by robertbu · Apr 11, 2014 at 08:51 PM

Take a look at my answer here:

http://answers.unity3d.com/questions/562443/turret-slerp-rotation-and-clamping.html

You will need to open up the comments at the bottom of my answer and scroll down. The code is just below the link to a package that demonstrates the script.

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 blokkiesotto · Mar 14 at 02:28 PM 0
Share

Hi... Can you pls help me pls modify my code here it is

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TurretRotater : MonoBehaviour { // e.g. 0.2 = slow, 0.8 = fast, 2 = very fast, Infinity = instant [Tooltip("If rotationSpeed == 0.5, then it takes 2 seconds to spin 180 degrees")] [SerializeField] [Range(0, 10)] float rotationSpeed = 0.5f;

 [Tooltip("If isInstant == true, then rotationalSpeed == Infinity")]
 [SerializeField] bool isInstant = false;

 Camera _camera = null;  // cached because Camera.main is slow, so we only call it once.

 void Start()
 {
     _camera = Camera.main;
 }

 void Update()
 {
     Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
     Quaternion targetRotation = Quaternion.LookRotation(ray.direction);

     if (isInstant)
     {
         transform.rotation = targetRotation;
     }     
     else
     {
         Quaternion currentRotation = transform.rotation;
         float angularDifference = Quaternion.Angle(currentRotation, targetRotation);
         // will always be positive (or zero)
         if (angularDifference > 0) transform.rotation = Quaternion.Slerp(
                                      currentRotation,
                                      targetRotation,
                                      (rotationSpeed * 180 * Time.deltaTime) / angularDifference
                                 );
         else transform.rotation = targetRotation;
     }
 }

}

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

22 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

Related Questions

Creating a multiple part turret what locks onto certain axis. 4 Answers

90 Degree stopping rotation on y-axis issue 0 Answers

"Free" rotation about a sphere 0 Answers

Rotation to align between two vectors but with preserved Camera Z axis 0 Answers

Quaternion Rotation around Local Axis 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