• 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 HNK91 · Jun 01, 2020 at 11:41 PM · 2d rotation

LocalRotation between two angles,localrotation between to angles 2D

Hi, I'm quite new to programming so my apologise if this is a straight forward fix but if someone can help me that would be great. I have this camera object I want to rotate between two points in my game.

At the moment I have it so it rotates the full 360 degrees. I have tried a few things out from other post that have been similar but nothing seems to work.

Here's what my code looks like so far.

public class EnemyCamera_Rotation : MonoBehaviour {

 public float degreesPerSec = 360f;
 public float rotationspeed;
 public float RotationAmount;
 public float min = -90;
 public float max = 90;
 void Start()
 {
 }

 void Update()
 {
     float rotAmount = rotationspeed * Time.deltaTime;
     float curRot = transform.localRotation.eulerAngles.z;
     transform.localRotation = Quaternion.Euler(new Vector3(0, 0, curRot + rotAmount));
    "  //RotationAmount = Mathf.Clamp(RotationAmount, min, max);" <<<<<<< I thought a clamp may fix my problem but didn't work
 }

}

Here's what it's currently doing - alt text

This is what I want it to do. alt text

Any help would be appreciated. Thanks :)

360-rotation.gif (451.6 kB)
cameramovement.gif (198.3 kB)
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
1
Best Answer

Answer by WaqasHaiderDev · Jun 02, 2020 at 03:47 AM

Hi, I am also not expert of this but what I experimented for nearly 2 hours and came up with this solution for you. It is working fine.

  using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TRY : MonoBehaviour
 {
     public float maxAngleLimit; // varies between 0 to 180
     public float minAngleLimit; // varies between 0 to 180 (sign does not matter)
     private float adjustedMinAngleLimit;
     public float rotationSpeed;
     private bool canChangeDirection = true;
     public float directionChangeController; // Adjust this number if movement is beyond range and jerky. Increase for high rotation speeds and decrease for low rotation speeds.
                                                    //typical values are from 2 - 20 but can be even larger for larger values
                                                    //this is basically the angle limit within which the bool canChangeDirection becomes true.
     
     // Start is called before the first frame update
     void Start()
     {
         adjustedMinAngleLimit = 360 - Mathf.Abs(minAngleLimit); //because unity reports angles always as positive angle by transform.localRotation.eulerAngles 
     }
 
     // Update is called once per frame
     void Update()
     {
         //In my case I am rotation 3d cube about local y axis. Change to whatever you need. 
 //Also I am rotating about the point passing through the trasform of the object. You can change it to //whatever position you want or you can also set pivot of your object by making it child to the empty //pivot object. Just attach this script to pivot
 
         transform.RotateAround(transform.position, Vector3.up, rotationSpeed * Time.deltaTime);
         if (canChangeDirection)
         {
             if (transform.localRotation.eulerAngles.y > maxAngleLimit && transform.localRotation.eulerAngles.y < adjustedMinAngleLimit)
             {
                 canChangeDirection = false;
                 rotationSpeed = -rotationSpeed;
             }
         }
        
         if (Mathf.DeltaAngle(transform.localRotation.eulerAngles.y,(maxAngleLimit+minAngleLimit)/2) < directionChangeController && Mathf.DeltaAngle(transform.localRotation.eulerAngles.y, (maxAngleLimit + minAngleLimit) / 2) > -directionChangeController)
         {
             canChangeDirection = true;
         }
         print(canChangeDirection);
     }
 
     
 
 }


Now coming to your problem, what was the issue with clamp approach. You were telling unity to clamp the value between -90 and +90. But there are 2 issues. First, unity reports -90 angle as +270 angle though it shows in inspector as -90 but if you print the value it is +270. So technically your angles are never going to become -90. Second is, let say it becomes -90, then what? you are not telling unity to change the direction of rotation once angle is less than -90. You are just telling unity not go beyond limits but you are not setting how these limits will be achieved or what if these limits are achieved.

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
1

Answer by HNK91 · Jun 02, 2020 at 11:03 AM

Thanks for the help GameHourStudio this helped out massively.

There was a few changes I did have to make as it was rotating around the wrong axis, I've added the changes below incase someone else needs help with this. ( needed to flip axis to .z for the rotation transforms and Vector3.up to Vector.forward. )

public class CameraMovement : MonoBehaviour

{ public float maxAngleLimit; public float minAngleLimit; private float adjustedMinAngleLimit; public float rotationSpeed; private bool canChangeDirection = true; public float directionChangeController;

 void Start()
 {
     adjustedMinAngleLimit = 360 - Mathf.Abs(minAngleLimit); //because unity reports angles always as positive angle by transform.localRotation.eulerAngles 
 }

 void Update()
 {


     transform.RotateAround(transform.position, Vector3.forward, rotationSpeed * Time.deltaTime);
     if (canChangeDirection)
     {
         if (transform.localRotation.eulerAngles.z > maxAngleLimit && transform.localRotation.eulerAngles.z < adjustedMinAngleLimit)
         {
             canChangeDirection = false;
             rotationSpeed = -rotationSpeed;
         }
     }

     if (Mathf.DeltaAngle(transform.localRotation.eulerAngles.z, (maxAngleLimit + minAngleLimit) / 2) < directionChangeController && Mathf.DeltaAngle(transform.localRotation.eulerAngles.z, (maxAngleLimit + minAngleLimit) / 2) > -directionChangeController)
     {
         canChangeDirection = true;
     }
     print(canChangeDirection);
 }



}

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 WaqasHaiderDev · Jun 02, 2020 at 01:50 PM 0
Share

Yes. I have mentioned in comments of the code that you need to adjust rotation axis yourself. Good to hear it helped you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 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

Related Questions

Rotate 2D turret toward target heading (LerpAngle) 2 Answers

Shooting at my mouse cursor. 2d Isometic game. 0 Answers

How to make a character move both vertically and horizontally on a plane? 1 Answer

How to fix enemy's rotation (2D) 1 Answer

2D Top Down, Making Player rotate & move with mouse? 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