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 V--R · Jul 18, 2018 at 06:02 PM · rotationscripting beginnerlookatrotation axislimitations

How would I add rotation limits to this?

I have the following script that I use to have my character's head face the target. However an error has come up where I've noticed that I don't currently have any limits for the rotation and so the character's head will regularly recreate scenes from the exorcist in order to look at the target. How would I set max/min limits to the various axis?

 using UnityEngine;
 using System.Collections;
 
 public class LookAtSlow : MonoBehaviour {
     public Transform target;
     public float degreesPerSecond;
 
     // Update is called once per frame
     void Update () {
 
         // The difference between target position and our position yields a direction
         Vector3 dirFromMeToTarget = target.position - transform.position;
 
         // Calculates a rotation where the direction 'dirFromMeToTarget' would be forward
         Quaternion lookRotation = Quaternion.LookRotation(dirFromMeToTarget);
 
         // Lerp from our current rotation to the desired 'lookRotation' at a rate of 'degreesPerSecond'
         transform.rotation =
             Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * (degreesPerSecond/360.0f));
     }
 }
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 Sgt_Spike · Jul 18, 2018 at 09:14 PM

Hey there. I think a 'Clamp' might do the trick. Try having a look at this page here 'https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html' and see if that's what you're looking for :D

Comment
Add comment · Show 15 · 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 V--R · Jul 18, 2018 at 09:38 PM 0
Share

What would I need to do to bring that into my setup. Sorry I'm really new to C# scripting.

avatar image Sgt_Spike V--R · Jul 18, 2018 at 09:48 PM 0
Share

To be honest I've never really used $$anonymous$$athf.Clamp myself :D. I think using something like this 'transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, -180, 180);' would work. What this code here should hopefully do is clamp the 'x' value of 'this' (assu$$anonymous$$g 'this' is your player) between -180 and 180, meaning that it cannot ever equal 270 for example. You will of course need to change 'x' to which ever coordinate it is that tilts the head left and right (probably x or z), and -180 and 180 can be changed to whatever you want. I haven't tried this code myself so let me know if it works or not!

avatar image V--R Sgt_Spike · Jul 21, 2018 at 02:42 PM 0
Share

Okay so I tried putting that into the script but I'm pretty sure I put it in the wrong spot since I'm getting an error now, also the axis that I want to limit the rotation of is the Y axis; I'd also like to limit the x a bit so that the head doesn't collapse into his chest if I were directly below him.

 using UnityEngine;
 using System.Collections;
 
 public class LookAtSlow2 : $$anonymous$$onoBehaviour {
     public Transform target;
     public float degreesPerSecond;
 
     // Update is called once per frame
     void Update () {
 
         // The difference between target position and our position yields a direction
         Vector3 dirFrom$$anonymous$$eToTarget = target.position - transform.position;
 
         // Calculates a rotation where the direction 'dirFrom$$anonymous$$eToTarget' would be forward
         Quaternion lookRotation = Quaternion.LookRotation(dirFrom$$anonymous$$eToTarget);
 
         transform.position.y = $$anonymous$$athf.Clamp(transform.position.y, -180, 180);
 
         // Lerp from our current rotation to the desired 'lookRotation' at a rate of 'degreesPerSecond'
         transform.rotation =
             Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * (degreesPerSecond/360.0f));
     }
 }
Show more comments

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

185 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why won't my model rotate up on X axis? 1 Answer

How to object rotate on a single axis towards another object? C# 2 Answers

Rotating Camera around Player's X-axis while rotating Player around its Y-axis (using mouse input) 0 Answers

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

how to reset back the rotation 0 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