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 Jorvan · Jun 18, 2016 at 01:07 AM · scripting problemcamera rotate

Need help limiting the x rotation of my camera and internet didn't helped me :(

Hi, I have been trying to fix it all day long, I searched in everywhere, but nothing worked :/ My problem is that I have my player with a cam attached to it and the rotations are fine, but want to limit the rotation of the x axis in the camera between -15 and 15, do you know how can I do it? D:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

 private Animator anim;
 private CharacterController controller;

 public float speed = 6.0f;
 public float turnSpeed = 60.0f;
 private Vector3 moveDirection = Vector3.zero;
 public float gravity = 20.0f;

 public Transform camTransform;

//(...)

 //Camera

 public float Y_ANGLE_MIN = -15.0f;
 public float Y_ANGLE_MAX = 15.0f;

 private float turnx = 0.0f;
 private float turnyc = 0.0f;


 void Update (){

     //Animations

     (...)

     //Moving

     (...)

     //Rotations

     turnx = Input.GetAxis ("Mouse X");
     turnyc = Input.GetAxis ("Mouse Y");

     transform.Rotate(0, turnx * turnSpeed * Time.deltaTime, 0);

     controller.Move(moveDirection * Time.deltaTime);
     moveDirection.y -= gravity * Time.deltaTime;
     
     camTransform.transform.Rotate (turnyc * turnSpeed * Time.deltaTime * -1, 0, 0); 
     
 }

}

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
Best Answer

Answer by Jorvan · Jun 18, 2016 at 09:37 PM

Great news: I SOLVED IT

I will share with you the correction.

Instead of Y_ANGLE_MIN and Y_ANGLE_MAX, I used:

private float rotcam = 0; public float rotcamax = 15f; public float rotcamin = -15f;

And in the Rotation portion I wrote this:

     turnx = Input.GetAxis ("Mouse X");
     turnyc = Input.GetAxis ("Mouse Y");

     transform.Rotate(0, turnx * turnSpeed * Time.deltaTime, 0);

     controller.Move(moveDirection * Time.deltaTime);
     moveDirection.y -= gravity * Time.deltaTime;

     rotcam += turnyc * turnSpeed * Time.deltaTime * tutu;
     rotcam = Mathf.Clamp (rotcam, rotcamin, rotcamax);

     camTransform.eulerAngles = new Vector3(rotcam, camTransform.eulerAngles.y);

Hope it helps someone :) Take care

Comment
Add comment · 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

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

68 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

Related Questions

Third person controller camera 1 Answer

Mouse Orbit Script Help 0 Answers

Why is my camera rolling around like a ball? 3 Answers

Camera Controller script as a child object,Camera Control as a Child Object for my Player 0 Answers

my script was written but does not want to work 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