Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 suuunly · Mar 27, 2015 at 05:03 PM · cameracustomboundsborder

CustomCameraController: Rotating my camera breaks my out of bounds code

Hey there guys, I've run into an issue that I can't seem to solve. I have a custom camera controller for an RTS like game I'm working on, and recently I tried to add boundaries, so that the camera can't go off the map.

The issue is, that when I move the camera to the edge it works just fine, but as soon as I add a small rotation to it, it breaks my logic. I know why this is happening, but I don't know how to fix it.

Basically, what is happening, is that I am moving the camera relative to the rotation and I am applying the Vector3.forward/Vector3.right for when I translate the camera regardless. Its worked fine until now.

The issue lies with these two:

 TranslateCamera(Vector3.forward, Input.GetAxisRaw("Vertical"), movementSpeed);
 TranslateCamera(Vector3.right, Input.GetAxisRaw("Horizontal"), movementSpeed);

and this method:

 private void TranslateCamera(Vector3 direction, float force, float additionalSpeed)
         {
             Vector3 translation = direction * force * additionalSpeed * amplify * Time.deltaTime;
     
             if (boundingBoxIsPresent)
             {
                 Vector2 position = new Vector2(rotationPoint.transform.position.x + translation.x, rotationPoint.transform.position.z + translation.y);
     
                 if (position.x > maxBounds.x || position.x < minBounds.x || position.y > maxBounds.y || position.y < minBounds.y)
                     translation = Vector3.zero;
             }
     
             rotationPoint.transform.Translate(translation, movementPoint.transform);
         }


How would I pass in the direction to make it work? or do you recommend a better way to handle the out of bounds, with out it bouncing back and forth when hits the wall (because as of now, it just hits the boundary and then stops perfectly)

If you feel like you are missing any context, then Here is my CameraController:

 /*
  *  Camera Controller 
  *  -----------------
  *  #Script uses controls provided by the InputManager
  *  This scripts adds controls to the camera. These controls include:
  *  - camera movement using the "Horizontal" and "Vertical" movements as well as "Mouse X" and "Mouse Y" controls.
  *  - camera rotation around a point*1 using the combination of "Fire3" and "Mouse X" (Required to hold down "Fire3" before using "Mouse X"
  *  - camera zoom using the "Mouse ScrollWheel"
  *  - speed up : using a custom Input called "Speed up" on the PC that is Shift and on the controller "ADD CONTROLLER HERE" 
  * 
  *  *1: This point is to be added as a parent of the camera (the object holding this script)
  * 
  * 
  */
 
 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
     // Speed properties
     public int movementSpeed = 10;
     public float speedIncrease = 2;
     public int mousePanMovementSpeed = 2;
     public int rotationSpeed = 20;
     public int scrollSpeed = 10;
 
     public float mousePanSensitivity = 1f;
 
     public GameObject BoundingBox;
 
     private Vector2 minBounds;
     private Vector2 maxBounds;
     private bool boundingBoxIsPresent = true;
 
     // World point properties
 
     /* This handles the constant movement, since when you rotate the camera the movement stays the same 
      * (that implies that moving forward makes it look like you're going, for example left). 
      * This object alters it to always stay constant. */
     private GameObject movementPoint; 
 
     private Transform rotationPoint;
     private float amplify;
     
     void Start()
     {
         rotationPoint = this.transform.parent;
         amplify = 1;
 
         movementPoint = new GameObject("inst_camera_movement");
         movementPoint.transform.position = this.transform.position;
 
         if(rotationPoint == null)
             Debug.LogError("!No parent was found on the Camera Object (with the CameraController component)! Camera needs a parent to operate! This parent is what controls the rotation (The camera rotates around this object");
 
         if(BoundingBox == null)
         {
             Debug.LogWarning("!Warning! No bounding box was found; the bounding box allows you to limit the camera movement to a specific distance (so not to go completely out of bound");
             boundingBoxIsPresent = false;
         } 
         else
         {
             Renderer mapBounds = BoundingBox.GetComponent<Renderer>();
 
             if (mapBounds == null)
                 Debug.LogError("ERROR: " + BoundingBox.name + " does not have a renderer!");
 
             minBounds = new Vector2(BoundingBox.transform.position.x - mapBounds.bounds.size.x / 2, BoundingBox.transform.position.z - mapBounds.bounds.size.z / 2);
             maxBounds = new Vector2(BoundingBox.transform.position.x + mapBounds.bounds.size.x / 2, BoundingBox.transform.position.z + mapBounds.bounds.size.z / 2);
 
             Debug.Log("Camera Bounds created!");
         }
 
         this.transform.LookAt(rotationPoint.transform); // Makes sure that the camera looks at the point at all time (for rotation purposes)
     }
 
 
     void Update ()
     {
         movementPoint.transform.position = this.transform.position;
 
         amplify = (Input.GetAxisRaw("Jump") == 1.0f) ? speedIncrease : 1;
 
         // ------- Controls the player movement
         TranslateCamera(Vector3.forward, Input.GetAxisRaw("Vertical"), movementSpeed);
         TranslateCamera(Vector3.right, Input.GetAxisRaw("Horizontal"), movementSpeed);
 
         // -------- Controls the scrolling mechanic
         this.transform.Translate(Vector3.forward * Input.GetAxisRaw("Mouse ScrollWheel") * scrollSpeed * amplify * Time.deltaTime, Space.Self);
 
         // --------- Controls the rotation mechanic
         if (Input.GetAxisRaw("Fire3") == 1.0f)
         {
             this.transform.RotateAround(rotationPoint.transform.position, Vector3.up, Input.GetAxis("Mouse X") * rotationSpeed * amplify * Time.deltaTime); // rotates around the y axis
             movementPoint.transform.RotateAround(rotationPoint.transform.position, Vector3.up, Input.GetAxis("Mouse X") * rotationSpeed * amplify * Time.deltaTime);
 
             this.transform.LookAt(rotationPoint.transform); // Makes sure that the camera looks at the point at all time (for rotation purposes)
         }
         else
         {
             //  --------- Camera movement based on mouse touching the edge
                 // This is written in this "slavery" fashion because this seems to make it run most smoothly
             if(Input.GetAxisRaw("Fire1") != 1f) // So that you can use the drag functionality in the MouseController without moving the camera meanwhile
             {
                 if (Input.mousePosition.x < mousePanSensitivity)
                     TranslateCamera(Vector3.left, mousePanMovementSpeed, 1);
                 else if (Input.mousePosition.x >= Screen.width - mousePanSensitivity)
                     TranslateCamera(Vector3.right, mousePanMovementSpeed, 1);
 
                 if (Input.mousePosition.y < mousePanSensitivity)
                     TranslateCamera(Vector3.back, mousePanMovementSpeed, 1);
                 else if (Input.mousePosition.y >= Screen.height - mousePanSensitivity)
                     TranslateCamera(Vector3.forward, mousePanMovementSpeed, 1);
             }
         }
 
         
 
     }
 
     private void TranslateCamera(Vector3 direction, float force, float additionalSpeed)
     {
         Vector3 translation = direction * force * additionalSpeed * amplify * Time.deltaTime;
 
         if (boundingBoxIsPresent)
         {
             Vector2 position = new Vector2(rotationPoint.transform.position.x + translation.x, rotationPoint.transform.position.z + translation.y);
 
             if (position.x > maxBounds.x || position.x < minBounds.x || position.y > maxBounds.y || position.y < minBounds.y)
                 translation = Vector3.zero;
         }
 
         rotationPoint.transform.Translate(translation, movementPoint.transform);
     }
 }
 


Thank you very much for going through this lengthy post c: May the Unity gods watch over you :p

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to stop view from scrolling when edge of view collides with a game object? 0 Answers

put prefab in top left of the camera 0 Answers

This camera controller is overriding it's default position when the boundary check is enabled? 0 Answers

Keeping the camera in bounds 1 Answer

Get screen bounds with **rotated** ortho camera?? 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