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 amfortas221 · Apr 13, 2020 at 03:52 PM · 2dmovementvectorlimitconstrain

How to limit the directions an object can move?

Hi, I need a way to limit the directions a 2d object can move. The object shouldn't be able to move in any directions in a curtain angle (red). alt text

Thx for any help

g1018.png (78.3 kB)
Comment
Add comment · Show 1
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 sacredgeometry · Apr 13, 2020 at 04:46 PM 0
Share

You can limit the possible rotation angles how do you deal with overshoot? do you want the values at the start of the impossible directions to jump to the end of them? Do you want it to just stop rotating and for them to have to turn all the way counter clockwise to move in that direction?

Can you give us more information about the behaviour please?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AsurZar · Apr 13, 2020 at 05:45 PM

Hi, you can define transformation (it has 3 directions), for each axe you can set angles min, max - this creates circle part on plane. Object has a transformation, you can use Z axe as direction, for check you needs to project this direction to plane and calculate angle (Cross product) between defined transform axe and projected vector, next you check this angle for min-max. It should be inside min-max for all 3 axes to make sure this direction is not allowed.

For 2D case you need only 1 axe.

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 Quickz · Apr 13, 2020 at 08:27 PM

I would make a method that takes in an angle from 0f to 360f and returns a constrained version of it based on the minimum and maximum allowed rotation. You can then assign that value to a transform's eulerAngle.

Cooked up a small utility class. Here's a sample: https://gist.github.com/Quickz/ade0c7018a6238ac11fa0754884cda11

 using System;
 using UnityEngine;
 
 public class RotationTest : MonoBehaviour
 {
     [Range(0f, 360f)]
     [SerializeField]
     private float maxRotation = 360f;
 
     [Range(0f, 360f)]
     [SerializeField]
     private float minRotation = 0f;
 
     private Camera mainCamera;
 
     private void Start()
     {
         mainCamera = Camera.main;
     }
 
     private void Update()
     {
         LookAtCursor();
         LimitRotation();
     }
 
     private void LookAtCursor()
     {
         Vector3 mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
         Vector3 direction = new Vector2(
             mousePosition.x - transform.position.x,
             mousePosition.y - transform.position.y);
 
         transform.up = direction;
     }
 
     private void LimitRotation()
     {
         float newRotation = RotationUtility.LimitRotation(
             transform.eulerAngles.z,
             minRotation,
             maxRotation);
 
         transform.eulerAngles = new Vector3(
             transform.eulerAngles.x,
             transform.eulerAngles.y,
             newRotation);
     }
 }
 
 public static class RotationUtility
 {
     /// <param name="rotation">
     ///  Value of 0f to 360f.
     ///  Rotation that will be limitted to a certain angle.
     /// </param>
     public static float LimitRotation(
         float rotation,
         float minRotation,
         float maxRotation)
     {
         if (minRotation > maxRotation)
         {
             throw new ArgumentException("Min rotation cannot exceed the max!");
         }
 
         if (rotation < 0f ||
             rotation > 360f)
         {
             throw new ArgumentException("Rotation has to be a value in range from 0f to 360f!");
         }
 
         if (rotation > maxRotation ||
             rotation < minRotation)
         {
             float rotationDistanceFromMaxValue = Mathf.Abs(Mathf.DeltaAngle(rotation, maxRotation));
             float rotationDistanceFromMinValue = Mathf.Abs(Mathf.DeltaAngle(rotation, minRotation));
 
             if (rotationDistanceFromMaxValue < rotationDistanceFromMinValue)
             {
                 return maxRotation;
             }
             return minRotation;
         }
 
         return rotation;
     }
 }

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

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

Issue with clamping player to camera view 2 Answers

Getting a 2d Sprite to move over time to an Array 1 Answer

Basic 2D movement C# - Key presses cancel eachother out 4 Answers

How do I make an Enemy charge through a point? 1 Answer

Constraining 2D movement to polygon surface 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