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
0
Question by Ellis · Oct 02, 2018 at 04:45 PM · camera rotateclampclamped rotation

Clamp horizontal rotation based on current direction

Hi!

So I'm a bit stuck atm.


I'm adding in a vault function into my game and currently that works fine! You move up to the vaultable object, press the "vault" button and it starts the animation for it and after it's finished the player object "teleports" to the position the camera is. No problem there!


The problem is that while the animation plays the player can move the camera around freely which can result in weird 360 spins on the vaultable object as well as problems with the "teleporting" of the player.

Now, I don't want to completly stop the player from being able to move the camera around as that would make the game feel much more locked up and clunky (I know, because I tried). What I want to do is just simply try and clamp the rotation of the player once the vault is initiated so that he/she can only turn the camera ~30 degrees horizontaly towards each side until the animation is finished, with the angle at which the vault was initiated from to be the "origin angle".


Any idea how I could do this in the easiest way possible?

Any help is appreciated!

Code for those who might want to see:


 // Camera Movement (Inside Update)
 if(mouse_Movement){
     //Vertical and Horizontal acceleration
     rot_horizontal = Input.GetAxis("Mouse X") * (cam_sensetivity_x / 100);
     rot_vertical = -Input.GetAxis("Mouse Y") * (cam_sensetivity_y / 100);
             
     //Applying Rotation
     transform.Rotate(0, rot_horizontal, 0);
     cam_main.gameObject.transform.Rotate(rot_vertical, 0, 0);
         //Calculating vertical rotation to a variable usable with mathf.clamp
         float angleX = cam_main.gameObject.transform.localEulerAngles.x;
         angleX = (angleX > 180) ? angleX - 360 : angleX;
                 
     //Restricting the cameras vertical rotation using mathf.clamp'
     cam_main.gameObject.transform.localEulerAngles = new Vector3( Mathf.Clamp( angleX, -60, 60), 0, 0);
 }    // ===============
         
 {//Vault
     //Check if vault key is pressed and if facing the correct way
     if(pc_vault && !pc_perfAnim && Input.GetButtonDown("Vault") && Vector3.Angle(transform.forward, trg_vault.transform.position - transform.position) < 70){
         StartCoroutine(PlayAnim("vault"));
     }
 }


 //Function that plays the right animation and "teleports" the player
 IEnumerator PlayAnim (string anim_type) {
     //Disable player movement while animation is playing
     TogglePCMovement();
     pc_perfAnim = true;
         
     //Performe the different types of Vault animations
     if(anim_type == "vault"){
         //Get animation name and start animation
         string anim_name = trg_vault.tag;
         anim_ctr.SetBool(anim_name, true);
             
         //Wait for animation to finish playing...
         yield return new WaitForEndOfFrame();
         yield return new WaitForSeconds(anim_ctr.GetCurrentAnimatorStateInfo(0).length);
             
         //Performe vault repositioning for each vault type
         if(anim_name == "Vault"){
             transform.position += transform.up *1f;
             transform.position += transform.forward * 1f;
         } else if(anim_name == "Vault Over"){
             transform.position += transform.forward * 1.5f;
         } else if(anim_name == "Climb"){
             transform.position += transform.up *2f;
             transform.position += transform.forward * 1f;
         } else if(anim_name == "Climb Over"){
             transform.position += transform.up *1f;
             transform.position += transform.forward * 1f;
         }
         //Stop playing animation, return to normal animation state
         anim_ctr.SetBool(anim_name, false);
     }
         
     //Enable player movement again
     pc_perfAnim = false;
     TogglePCMovement();
         
     //Final return for the sake of not getting errors :P
     yield return new WaitForEndOfFrame();
 }
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

0 Replies

· Add your reply
  • Sort: 

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

90 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

Related Questions

I can't figure out how to clamp it 2 Answers

How to Math.f Clamp this 0 Answers

Camera Clamping Third Person? 0 Answers

Problems with turret rotation clamp 1 Answer

Clamp issue 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