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 JoshPriceGames · Jun 13, 2019 at 06:46 AM · camerarotationcamera rotatelimit

Help With Arrow Camera Script

I have a simple code that can rotate an object up and down, and I use it on my camera

 var speed = 30;
 
 
  function Update () {
  
   if (Input.GetButton("Camera Up"))
       transform.Rotate(-Vector3.right * speed * Time.deltaTime);
 
   if (Input.GetButton("Camera Down"))
       transform.Rotate(Vector3.right * speed * Time.deltaTime);
 
  }

I have been trying to implement two things

  1. Limiting the rotation so the player can't spin it 360 degrees (I've seen ways to do it using Mouse axis but I don't know how to do it with Button presses)

  2. When I'm not pressing either "camera up" or "camera down", have the camera slide back to normal (meaning, slide the X rotation back to 0)

(Please don't just send a link to the Transform.eulerAngles page or the Transform.localEulerAngles page. I have already read these and I still can't figure it out. It wont be helpful)

EDIT: btw the code above is written in JavaScript

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JoshPriceGames · Jun 13, 2019 at 10:15 PM

I managed to get a working script, with help from Meishin.

I changed it to C#, and altered it so that It doesn't snap the other rotation axis' when I press up/down.

Seems to work so far :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamUpDown : MonoBehaviour {
 
     public float speed = 10;
     float angleV;
     public float minVerticalAngle = -45;
     public float maxVerticalAngle = 45;
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetButton("Camera Up"))
         {
             // Define angleV as private float in class
             angleV += speed * Time.deltaTime ; // angleV -= if "Camera Down"
             // Set vertical movement limit
             angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
             // Calculate rotation
             Quaternion targetRotation = Quaternion.Euler(-angleV, transform.eulerAngles.y, transform.eulerAngles.z);
             // Apply
             transform.rotation = targetRotation;
         }
 
 
         if (Input.GetButton("Camera Down"))
         {
             // Define angleV as private float in class
             angleV -= speed * Time.deltaTime ; // angleV -= if "Camera Down"
             // Set vertical movement limit
             angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
             // Calculate rotation
             Quaternion targetRotation = Quaternion.Euler(-angleV, transform.eulerAngles.y, transform.eulerAngles.z);
             // Apply
             transform.rotation = targetRotation;
         }
     }
 }
 

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

Answer by Meishin · Jun 13, 2019 at 07:21 AM

Hello JoshPriceGames,

Assuming your camera is a 1st person one (otherwise your current code cannot work) ; - To clamp the rotation you could do something like ;

 if (Input.GetButton("Camera Up"))
 {
    // Define angleV as private float in class
    angleV += speed * Time.deltaTime ; // angleV -= if "Camera Down"
    // Set vertical movement limit
     angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
    // Calculate rotation
    Quaternion targetRotation = Quaternion.Euler(-angleV, 0, 0);
    // Apply
    transform.rotation = targetRotation;
 }

  • Regarding your 2nd point, nothing in your current code does that. I either comes from an other script, or your camera parenting positioning, or the physics (can't help you much here)

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 JoshPriceGames · Jun 13, 2019 at 09:46 AM 0
Share

I tried this code out, got "UCE0001: ';' expected. Insert a semicolon at the end." Of course, even if I added the ; , I'd get errors. $$anonymous$$y original script is in JavaScript, so if that is C# stuff, it's probably why it'ts not working

Regarding your 2nd point, nothing in your current code does that.

I know currently there is nothing in my script doing either of the things I asked, I am asking for help since I don't know how to do it :)

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

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

Related Questions

Forward and back movements with a camera emulating an isometric view 1 Answer

Mouse axes based on position, not movement. 1 Answer

Camera rotates when I run into a wall 0 Answers

Object rotates on an axis I don't want 1 Answer

Limit Y Axis transform.RotateAround 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