Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Artpen · Jun 19, 2021 at 03:40 PM · movementvector3clamp

Limit Camera movement by distance from target

Hi guys, A quick question...

I have a target and a Camera which moves back and forth (Vector3.forward) but I want to limit the movement with min and max distance from target. When distance from target to camera is 1000 it stops, when distance 6000 camera stops.

What I did so far is using Vector3.ClamMadnitude , but it clamps only maximum distance like radius.

How to restrict camera movement ?

 // Move Camera
             cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
             transform.Translate(cameraMove * Time.deltaTime);
 
             // Find minimal camera position
             Vector3 minPosition = (transform.position - cameraRig.position).normalized * minZoomDistance;
             // Find maximal camera position
             Vector3 maxPosition = (transform.position - cameraRig.position).normalized * maxZoomDistance;
             // Find distance / radius where camera can go
             float movementRange = (maxPosition - minPosition).magnitude;
             // Calculate the distance of the new position from the center point
             Vector3 offset = transform.position - minPosition;
             // Clamp the length to the specified radius / distance
             transform.position = minPosition + Vector3.ClampMagnitude(offset, movementRange);
Comment
Add comment · Show 2
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 Artpen · Jun 19, 2021 at 04:12 PM 0
Share

I looked into using Mathf.Clamp, but not sure how to use it in this situation. As I need to clamp each axis (x ,y and z)

avatar image Artpen · Jun 19, 2021 at 08:26 PM 0
Share

I tried to use Mathf.Clamp, but it just moves my camera to a $$anonymous$$ location? // Move Camera cameraMove = Vector3.forward touchDistanceDifference zoomSpeed; transform.Translate(cameraMove * Time.deltaTime);

             Vector3 clampedPosition = transform.position;
 
             // Apply movement boundaries
             clampedPosition.x = Mathf.Clamp(clampedPosition.x, -400, -2095);
             clampedPosition.y = Mathf.Clamp(clampedPosition.y, 701, 4089);
             clampedPosition.z = Mathf.Clamp(clampedPosition.z, -628, -3562);
 
             transform.position = clampedPosition;

1 Reply

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

Answer by DenisIsDenis · Jun 20, 2021 at 04:28 AM

In this case, it is more convenient to implement your "ClampMagnitude" based on two conditions and a vector directed from the target to the camera:

 using UnityEngine;
 
 public class ClampDistance : MonoBehaviour
 {
     public Transform target; // Target object
     public float minDistance = 5, maxDistance = 15;
 
     void Update()
     {
         // vector directed from the target to the camera
         Vector3 direction = transform.position - target.position;
 
         // Our ClampMagnitude
         if (direction.magnitude < minDistance)
         {
             direction = direction.normalized * minDistance;
         }
         if (direction.magnitude > maxDistance)
         {
             direction = direction.normalized * maxDistance;
         }
 
         transform.position = target.position + direction;
     }
 }

You need to assign this script to the camera and then it will be between the minimum and maximum distance from the target.

Comment
Add comment · Show 2 · 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 Artpen · Jun 20, 2021 at 02:25 PM 0
Share

Hi @DenisIsDenis thank you for your answer. To be honest I tried it before as well. The problem I have I can't limit the movement ... Even with if statements one allows for another to execute the code.

Not sure how to limit camera m movement.

 // Get vector from target to camera
             Vector3 limitVector = transform.position - cameraRig.position;
             Debug.Log(limitVector.magnitude);
 
             if(limitVector.magnitude > $$anonymous$$ZoomDistance) 
             {
                 // Move Camera
                 cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
                 transform.Translate(cameraMove * Time.deltaTime);
             }
 
             if (limitVector.magnitude < maxZoomDistance) 
             {
                 // Move Camera
                 cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
                 transform.Translate(cameraMove * Time.deltaTime);
             }
avatar image Artpen · Jun 20, 2021 at 02:50 PM 1
Share

Ok Got it to work :) Add a touch limit. to if statement // Get vector from target to camera Vector3 limitVector = transform.position - cameraRig.position; Debug.Log(limitVector.magnitude);

             if(limitVector.magnitude > $$anonymous$$ZoomDistance && touchDistanceDifference > 0) 
             {
                 // Move Camera
                 cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
                 transform.Translate(cameraMove * Time.deltaTime);
             }
 
             if (limitVector.magnitude < maxZoomDistance && touchDistanceDifference < 0)
             {
                 // Move Camera
                 cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
                 transform.Translate(cameraMove * Time.deltaTime);
             }

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

194 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

Related Questions

Limit object position which is moved by accelerometer 0 Answers

Smooth motion physics Playmaker 0 Answers

How to set maximum distance for a player going left and right? 1 Answer

I am unable to update MoveTowards target during runtime. Object move only to first target. 0 Answers

Move Forward Locally While Still having Collision 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