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 Dec 18, 2016 at 09:04 PM by hellojbb1234.
avatar image
0
Question by hellojbb1234 · Dec 18, 2016 at 11:31 AM · camerajoystickcamera rotatemathf.clamp

How do I get this to Rotate properly?

I have this code here and I was trying to make the rotation be clamped so it can't rotate all the way around, but now its just rolling up to a certain point. So how do I get it to where it looks up and down and side to side only up to a certain point on each side.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class CameraRotate : MonoBehaviour
 {
 
     public float speedH = 2.0f;
     public float speedV = 2.0f;
     private Camera maincamera;
     private float yaw = 0.0f;
     private float pitch = 0.0f;
     private float rotationZ = 0f;
     private float rotationX = 0f;
    
     void Start()
     {
         maincamera = Camera.main;
     }
     void Update()
     {
         maincamera.transform.LookAt(transform);
 
         float speed = 10.0f;
 
         rotationX = CrossPlatformInputManager.GetAxis("Horizontal_2") * speed;
         rotationZ = CrossPlatformInputManager.GetAxis("Vertical_2") * speed;
         rotationX = Mathf.Clamp(rotationX, -90, 90);
         rotationZ = Mathf.Clamp(rotationZ, -90, 90);
         transform.localEulerAngles = new Vector3(rotationX, transform.localEulerAngles.y, rotationZ);
     }
 }

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
0

Answer by ProtoTerminator · Dec 18, 2016 at 02:53 PM

Parent the camera to another gameobject. That gameobject will be your reference to your clamp. Then, simply use Mathf.Clamp to clamp your angle

 camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(negativeAngle, positiveAngle), Mathf.Clamp(negativeAngle, positiveAngle), Mathf.Clamp(negativeAngle, positiveAngle));



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 hellojbb1234 · Dec 18, 2016 at 03:20 PM 0
Share
 maincamera.transform.localEulerAngles = new Vector3(rotationX, transform.localEulerAngles.y, rotationZ);

Like this? I decided on this option ins$$anonymous$$d however I would still like to be able to stop it from rotating to far. but otherwise it works except for the fact that the camera doesn't stop when it reaches a certain point.

  transform.Rotate(Vector3.up, $$anonymous$$athf.Clamp((CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal_2") * speed), -90, 90));
         transform.Rotate(Vector3.left, $$anonymous$$athf.Clamp((CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical_2") * speed), -90, 90));

I have another question too related to this one how do clamp the basic rotation of any object like if I want the x to rotate a little bit on the ground the I want that clamped so it doesn't rotate so much as to fall on its back.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

How to fix weird camera rotation? 0 Answers

Camera moving independent of players rotation 1 Answer

Return camera to original rotation values Logic 1 Answer

Limiting vertical camera 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