Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Halo500 · Feb 10, 2014 at 10:04 PM · joystickcamera rotatey axis

Limit Vertical (Y Position) of Player Camera on Joysticks?

Hello! I have joysticks from a very well made asset (https://www.assetstore.unity3d.com/#/content/12373) and the problem I have with it is the Player's camera can rotate in a full 360°. I can rotate camera upside down and everything!

I'm trying to limit the Y-axis rotation of the Joysticks. The camera-rotation section is at the very bottom of the script. Can somebody tell me what I need to adjust to limit the Camera's Y-Position Rotation to (-60f,60f) cause when I just add that in, I can't move the Player's Camera AT ALL. Here is the script:

 [RequireComponent(typeof(CharacterController))]
 public class UMJDemo_PlayerController : MonoBehaviour {
     
     #region Move_Controller_Parameters Vars
     public UMJDemo_Joystick MoveJoystick;
     public UMJDemo_Joystick RotateJoystick;
     public UMJDemo_Button ShootButton;
     
     public float CameraSmooth = 0.075f;
     public float MoveSpeed = 15f;
     public float RotateSpeed = 15f;    
     public float MinTForceToMove = 10f;
     public float MinTForceToRotate = 10f;
     public float MinTForceToShoot = 25f;
     
     //public UMJDemo_Weapon MainWeapon;
     private CharacterController PlayerController = null;
     private Transform PlayerTransform = null;
     private Vector3 Movement;
     #endregion    
     
     #region Camera_Parameters Vars
     public bool FPSCamera = false;
     public bool MultiJoyCam = false;
     public float CameraBiasFactor = 3.35f;    
     private Transform CameraTransform = null;    
     #endregion    
     
     #region Awake
     void Awake () 
     {    
         PlayerTransform = transform;
         PlayerController = GetComponent<CharacterController>();                
         CameraTransform = Camera.main.transform;
     }
     #endregion    
     
     #region Update
     void Update () 
     {    
         if ( MoveJoystick.JSK_TouchForce > MinTForceToMove ) PlayerMovement( MoveJoystick.JSK_DirectionNormalized.y, MoveJoystick.JSK_DirectionNormalized.x, MoveJoystick.JSK_TouchForce );
         else PlayerController.Move( Physics.gravity * Time.deltaTime * 3f );                
         
         if ( RotateJoystick.JSK_TouchForce > MinTForceToRotate ) PlayerRotation( RotateJoystick.JSK_Direction.y, RotateJoystick.JSK_Direction.x, RotateJoystick.JSK_TouchForce );
         //if ( !FPSCamera && RotateJoystick.JSK_TouchForce > MinTForceToShoot || FPSCamera && ShootButton.Pressed ) MainWeapon.Shooting();
                 
         if ( !MultiJoyCam )CameraMovement ();
         if ( FPSCamera ) CamRot_FPS();
     }
     #endregion
     
     #region PlayerMovement
     private void PlayerMovement( float MoveFactor_X, float MoveFactor_Y, float MoveStrength )
     {        
         if ( FPSCamera )
         {                        
             Movement += ( PlayerTransform.forward * MoveFactor_X ) * MoveSpeed * MoveStrength / 100f;
             Movement += ( PlayerTransform.right * MoveFactor_Y ) * MoveSpeed * MoveStrength / 100f;
         }
         else
         {
             Movement = new Vector3 ( -MoveFactor_X, 0, MoveFactor_Y ) * MoveSpeed * MoveStrength / 100f;                
         }    
         
         Movement *= Time.deltaTime;
         Movement += Physics.gravity * Time.deltaTime * 3f;
         PlayerController.Move( Movement );
         if ( RotateJoystick.JSK_TouchForce < MinTForceToMove ) PlayerRotation( MoveJoystick.JSK_Direction.y, MoveJoystick.JSK_Direction.x, MoveStrength );        
     }
     #endregion
     
     #region PlayerRotation
     private void PlayerRotation( float RotateFactor_Y, float RotateFactor_X, float RotateStrength )
     {
         Vector3 TargetDirection = new Vector3 ( -RotateFactor_Y, 0f, RotateFactor_X );
         Quaternion TargetRotation = Quaternion.LookRotation ( TargetDirection, Vector3.up );
         Quaternion NewRotation = Quaternion.Lerp ( PlayerTransform.rotation, TargetRotation, ( RotateSpeed / 100f * RotateStrength ) * Time.deltaTime );        
         if ( !FPSCamera ) PlayerTransform.rotation = NewRotation;        
     }
     #endregion    
     
     float PositionX = 0f;
     float PositionY = 0f;
     #region CameraMovement
     private void CameraMovement()
     {    
         if (!FPSCamera ) { PositionX = 7.8f; PositionY = 11.5f; }
         else PositionY  = 0.8f;
         
         Vector3 CameraVelocity = Vector3.zero;
         Vector3 CameraTargenPosition = new Vector3 ( PlayerTransform.position.x + PositionX, PlayerTransform.position.y + PositionY, PlayerTransform.position.z );
         Vector3 NewCameraTargenDirection = new Vector3 ( ( -RotateJoystick.JSK_DirectionNormalized.y * RotateJoystick.JSK_TouchForce / 100f ) * CameraBiasFactor, 
                                              0f, (  RotateJoystick.JSK_DirectionNormalized.x * RotateJoystick.JSK_TouchForce / 100f ) * CameraBiasFactor );        
                         
         if ( !FPSCamera ) CameraTargenPosition += NewCameraTargenDirection;        
         CameraTransform.position = Vector3.SmoothDamp( CameraTransform.position, CameraTargenPosition, ref CameraVelocity, CameraSmooth );
     }
     #endregion
     
     float RotationX = 0f;
     float RotationY = 0f;
     private void CamRot_FPS()
     {
 //        if ( RotateJoystick.JSK_Touch.phase != TouchPhase.Stationary )
 //        {        
             RotationX += Mathf.Clamp ( RotateJoystick.JSK_DirectionNormalized.x * RotateJoystick.JSK_TouchForce / 100f, -1, 1 );
             RotationY += Mathf.Clamp ( RotateJoystick.JSK_DirectionNormalized.y * RotateJoystick.JSK_TouchForce / 100f, -1, 1 );    
 
             Quaternion FPS_Rotation = Quaternion.Euler( -RotationY * 5f, RotationX * 5f, 0f );        
             CameraTransform.rotation = FPS_Rotation;
             PlayerTransform.rotation = FPS_Rotation;
 //        }
     }
 }
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

18 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

Related Questions

How do I get this to Rotate properly? 1 Answer

Limit Y axis rotation of camera 1 Answer

SidescrollControl X and Y Axis Movement Problem 1 Answer

If Statement Triggers No Matter What! 1 Answer

Joystick to movement and rotate camera same time 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