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 AlphaGarg8447 · Aug 29, 2015 at 10:48 PM · c#rotationscripting problemrotation axisrotation detection

Problems with limiting angles

Hello, I am in dire need of help here. What I'm trying to achieve is some kind of limit to an object's rotation. I'm working on a game about table soccer where roulettes aren't allowed on some tables, but to limit the rotation of the handles just seems impossible. I've looked at other questions but I couldn't implement none of them in it. The handles right now are just 1 floating rectangle in the middle of the field, for testing reasons. The code is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class ctrl : MonoBehaviour {
     
     public GameObject handle1;
     public GameObject handle2;
     public GameObject handle3;
     public GameObject handle4;
     public float handler = 0.00f;
     
     // Update is called once per frame
     void Update () {
 
         //Everything needs a limit; even the rotation of the handles.
 
         //The first handle's limits
         if (handle1.transform.rotation.x < -80)
         {
             handle1.transform.rotation = Quaternion.Euler(-80,0,0);
             Debug.Log("gud1");
         }
         if (handle1.transform.rotation.x > 110)
         {
             handle1.transform.rotation = Quaternion.Euler(110,0,0);
         }
 
         //The second handle's limits
         if (handle2.transform.rotation.x < -80)
         {
             handle2.transform.rotation = Quaternion.Euler(-80,0,0);
         }
         if (handle2.transform.rotation.x > 110)
         {
             handle2.transform.rotation = Quaternion.Euler(110,0,0);
         }
         
         //The third handle's limits
         if (handle3.transform.rotation.x < -80)
         {
             handle3.transform.rotation = Quaternion.Euler(-80,0,0);
         }
         if (handle3.transform.rotation.x > 110)
         {
             handle3.transform.rotation = Quaternion.Euler(110,0,0);
         }
         
         //The fourth handle's limits
         if (handle4.transform.rotation.x < -80)
         {
             handle4.transform.rotation = Quaternion.Euler(-80,0,0);
         }
         if (handle4.transform.rotation.x > 110)
         {
             handle4.transform.rotation = Quaternion.Euler(110,0,0);
         }
 
         handle1.transform.Rotate(handler,0,0);
         handle2.transform.Rotate(handler,0,0);
         handle3.transform.Rotate(handler,0,0);
         handle4.transform.Rotate(handler,0,0);
         
         //Left
         if (Input.GetAxis("Mouse X")<0)
         {
             handler += 2.50f;
             Debug.Log("Mouse moved left");
         }
         if (handler > 0)
         {
             handler -= 1.25f;
         }
         if (handler > 5.00f)
         {
             handler = 5.00f;
         }
         //Right
         if (Input.GetAxis("Mouse X")>0)
         {
             handler -= 2.50f;
             Debug.Log("Mouse moved right");
         }
         if (handler < 0)
         {
             handler += 1.25f;
         }
         if (handler < -5.0f)
         {
             handler = -5.0f;
         }
     }
 }

I've tried that but nothing seems to change. I've also tried transform.rotation.x but it just gives errors out because apparently that statement is read-only and can't be edited separatedly.

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 Maruder · Sep 14, 2015 at 02:10 AM

@Marcos42563 From what I see in the code, it appears to me you are trying to rotate the paddle about the x-axis and are trying to constrain limits at how far each handle can turn or rotate.

I don't know if this will help but I do have code that you can probably look over that deals with limiting angle rotations here:

     public float turn_speed;
     private float turn_velocity;
     public float minAngle,maxAngle;
     //angles to modify for turning.
     private float x,y,z;
 
     void Start()
     {
         //set x,y,z angles to modify
         x = transform.eulerAngles.x;
         y = transform.eulerAngles.y;
         z = transform.eulerAngles.z;
 
         //convert frames per second to units per second movement.
         turn_velocity = turn_speed * Time.deltaTime;
     }
 
     void Update()
     {
         RotateArrow ();
         LaunchBall ();
     }
     void RotateArrow()
     {
         //right
         if(Input.GetKey(KeyCode.D))
         {
             z -= turn_velocity;
         }
         //left
         if(Input.GetKey (KeyCode.A))
         {
             z += turn_velocity;
         }
         
         //restrict angle of rotation
         z = Mathf.Clamp (z, minAngle, maxAngle);
         
         //set rotate Angle to game Object
         transform.eulerAngles = new Vector3 (x, y, z);
     }

In the unity editor, you can set the minAngle and maxAngle each handle can turn as they are public. Furthermore, if you look at the line under the comment //restrict angle of rotation, you can use Mathf.Clamp(angle to rotate about,min Angle,max Angle) to put constraints on how much a paddle can rotate.

Instead of rotating about the z-axis, you can rotate about the x-axis where your following code may look like this:

 x = Mathf.Clamp(x,minAngle,maxAngle);



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

28 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

Related Questions

Why won't my model rotate up on X axis? 1 Answer

How to make a gameObject's Y rotation being another gameObjects' X rotation 1 Answer

.rotation doesn't seem to work, can't shoot projectile at mouse coordinates 0 Answers

Searching a function to get the rotation of one Vector3 in relation to another 1 Answer

How to deal with stupid rotation system? 2 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