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 /
This question was closed Oct 09, 2016 at 10:35 PM by xZEUS99x for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by xZEUS99x · May 19, 2016 at 04:56 AM · clampturretmouse positionclamped rotationmouse aim

Problems with turret rotation clamp

I've got a little game I'm working on, and I've got a turret set up on another gameobject. This turret is rotated by the mouse. They are also clamped so they can only rotate so far. However, I have a slight issue. When I move the mouse around passed the clamp, they don't move, however as soon as I turn the mouse back they do again. Just that they no longer are looking at the mouse. I have a vague idea of what's going on, they are following the increase of the mouse axis but once I start to decrease it they instantly start to move, despite the mouse not being what they are facing. However, I'm a bit unsure as to how to solve this issue. Any help would be greatly appreciated.

Here's the code: (Note: I know one of the rotation axis is not used, but I may change this in the future so it is.)

 using UnityEngine;
 using System.Collections;
 
 public class TurretBaseController : MonoBehaviour {
 
     public GameObject playerPos;
     public Transform projSpawn;
     public GameObject projectile;
 
     public float minY = -90f;
     public float maxY = 90f;
     public float sensY = 600f;
     public float minX = -660f;
     public float maxX = 360f;
     public float sensX = 300f;
     float rotationY = 0f;
     float rotationX = 0f;
     public float rotSmooth;
 
     //float rotationYlim = 0f;
     //float rotationXlim = 0f;
 
     public float offSetX;
     public float offSetY;
     public float offSetZ;
 
     public Quaternion rotation;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         //axis the object can rotate along
         rotationY += Input.GetAxis ("Mouse X") * sensY * Time.deltaTime;
         rotationX += Input.GetAxis ("Mouse Y") * sensX * Time.deltaTime;
 
         //rotation limits
         rotationY = Mathf.Clamp (rotationY, minY, maxY);
         rotationX = Mathf.Clamp (rotationX, minX, maxX);
 
 
         //assigns the rotation
         rotation = Quaternion.Euler(0, rotationY, 0);
 
         //does the rotation
         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSmooth);
     }
 }
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

  • Sort: 
avatar image
1

Answer by Mikilo · May 19, 2016 at 08:42 AM

Hello,

You clamp rotation[X|Y] lines 42 and 43, as far as you are higher or lower than the max or the min, you are not gonna move. The moment you move toward the opposite direction, rotation[X|Y] will not be clamped anymore.

Maybe you should clamp at line 47 like that:

      rotation = Quaternion.Euler(0, Mathf.Clamp (rotationY, minY, maxY), 0);

Indeed, remove the previous clamps.

This way, you only clamp at the point you use the value. The value is still free to be higher or lower than the clamping limits.

Comment
Add comment · Show 18 · 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 xZEUS99x · May 19, 2016 at 10:29 PM 0
Share

That works perfectly! Thank you!

There is another tiny thing, if I change it to localRotation, turning the object which the turret is on means it no longer points towards the mouse. Rather, it keeps pointing the way it was locally. I'd imagine this is something to do with the way I've handled it actually rotating towards the mouse, but I'm not exactly sure how I'd fix it.

This is all the changes I've made to the code:

 //rotation limits
             //rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$Y, maxY);
             //rotationX = $$anonymous$$athf.Clamp (rotationX, $$anonymous$$X, maxX);
 
         //assigns the rotation
         //rotation = Quaternion.Euler(0, rotationY, 0);
         rotation = Quaternion.Euler(0, $$anonymous$$athf.Clamp (rotationY, $$anonymous$$Y, maxY), 0);
 
         //does the rotation
         transform.localRotation = Quaternion.Slerp (transform.localRotation, rotation, rotSmooth);
avatar image Mikilo xZEUS99x · May 20, 2016 at 04:37 AM 0
Share

Nice! =D

But where is your question?

If you update localRotation, yes it is local. If it worked with rotation, why changing to localRotation?

avatar image xZEUS99x Mikilo · May 29, 2016 at 03:43 PM 1
Share

Since it's a bit hard to describe, I created a quick little video showing the problem, hopefully it will be more clear what the problem is.

https://www.youtube.com/watch?v=I3UNkjAwd70

Show more comments

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Clamping a wrapping rotation. 6 Answers

Clamp issue 1 Answer

Turrets acting oddly when clamped 1 Answer

How to use mathf.clamp? 2 Answers

Turret rotation clamp. 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