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 /
This question was closed Aug 31, 2015 at 09:15 PM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
5
Question by Ashky · Sep 29, 2014 at 09:43 AM · c#camera3dresolutionperspective

How to keep an object within the camera view?

Hello Unity Community!

I am currently working on a 3D space combat game (chicken invaders styled). I would like to know how to always keep an object within the camera view, regardless of resolution. Do I need to change the camera's field of view according to each resolution? I am using a perspective camera. Should I switch to an orthographic camera instead?

Thank you very much!

-Ashky

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 bobin115 · Sep 29, 2014 at 10:01 AM 0
Share

if its render distance you're talking about just click on your camera and adjust the clipping planes

2 Replies

  • Sort: 
avatar image
31
Best Answer

Answer by robertbu · Sep 29, 2014 at 03:19 PM

This will keep the pivot point a game object visible:

 using UnityEngine;
 using System.Collections;
 public class Example : MonoBehaviour {
 
     void Update() {
         Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
         pos.x = Mathf.Clamp01(pos.x);
         pos.y = Mathf.Clamp01(pos.y);
         transform.position = Camera.main.ViewportToWorldPoint(pos);
     }
 }

You can clamp it tighter if needed. Example:

 pos.x = Mathf.Clamp(pos.x, 0.1, 0.9);

Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right. This code forces an object to have a viewport coordinate in the (0,0) to (1,1) range, and therefore to be on the screen.

Comment
Add comment · Show 8 · 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 Ashky · Sep 29, 2014 at 05:08 PM 0
Share

This works great, thank you very much!

avatar image Mystomex · Aug 05, 2016 at 09:45 PM 0
Share

Thank you robertbu so very much, i have been pulling my hair out trying to keep my playerobject inside a 2d camera that rotates on the z axis, this work perfectly and you just opened my eyes to many more possibilities.

avatar image Erwin32 · Oct 26, 2016 at 03:10 PM 0
Share

This is a short and simple solution that works perfectly. Thanks!

avatar image gdp2 · Feb 15, 2018 at 12:13 AM 0
Share

Thank you, this can definitely be used in many ways to keep an object in view.

avatar image alexhapki · Apr 13, 2020 at 06:07 AM 0
Share

Great solution! $$anonymous$$ine was longer, so I changed to yours on my current videogame. Thank you.

Show more comments
avatar image
0

Answer by sysameca · Sep 29, 2014 at 11:05 AM

I assume you want the object to move independent of the screen aspect ratio:

 using System;
 using System.Collections.Generic;
 using System.Collections;
 using UnityEngine;
 
 
 public class Controller : MonoBehaviour
 {
     public float speed = 0.0f;
 
     // The clamp margins
     public float
         clampMarginMinX = 0.0f,
         clampMarginMaxX = 0.0f,
         clampMarginMinY = 0.0f,
         clampMarginMaxY = 0.0f;
 
     // The minimum and maximum values which the object can go
     private float
         m_clampMinX,
         m_clampMaxX,
         m_clampMinY,
         m_clampMaxY;
 
     private void Start()
     {
         // Get the minimum and maximum position values according to the screen size represented by the main camera.
         m_clampMinX = Camera.main.ScreenToWorldPoint(new Vector2(0 + clampMarginMinX, 0)).x;
         m_clampMaxX = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width - clampMarginMaxX, 0)).x;
         m_clampMinY = Camera.main.ScreenToWorldPoint(new Vector2(0, 0 + clampMarginMinY)).y;
         m_clampMaxY = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height + clampMarginMaxY)).y;
     }
 
     private void Update()
     {
         Vector3 direction = Vector3.zero;
 
         // Going left
         if (Input.GetKey(KeyCode.A))
         {
             direction = Vector2.right * -1;
         }
 
         // Going right
         else if (Input.GetKey(KeyCode.D))
         {
             direction = Vector2.right;
         }
 
         if (transform.position.x < m_clampMinX)
         {
             // If the object position tries to exceed the left screen bound clamp the min x position to 0.
             // The maximum x position won't be clamped so the object can move to the right.
             direction.x = Mathf.Clamp(direction.x, 0, Mathf.Infinity);
         }
 
         if (transform.position.x > m_clampMaxX)
         {
             // Same goes here
             direction.x = Mathf.Clamp(direction.x, Mathf.NegativeInfinity, 0);
         }
 
         transform.position += direction * (Time.deltaTime * speed);
     }
 
 }

Basically the idea is to clamp the movement direction so the object can't exceed the screen limits.I didn't wrote the code for up and down movement but basically it's almost the same.The clamp margins can define the movement square

![alt text][1]


clampexample.png (25.0 kB)
Comment
Add comment · Show 3 · 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 Ashky · Sep 29, 2014 at 03:05 PM 0
Share

I have placed this code inside my script, however, I think it does not work properly. This is how my current script looks: http://pastebin.com/cWch67aq . Only m_clamp$$anonymous$$inY and m_clamp$$anonymous$$axY are calculated, both being 50. The rest of the variables are 0, and the object does not move.

avatar image sysameca · Sep 30, 2014 at 09:13 PM 0
Share

Sorry for the late response but robert actually answered your question with simpler method.The script works fine however you are setting the public margin variables through the script which will directly unset them to 0 or whatever the value is in the inspector.When you inherit from $$anonymous$$onobehaviour the public variables need to be set through the inspector in order the change to take effect.

avatar image alexhapki · Sep 04, 2020 at 01:11 PM 0
Share

Hi guys, Sharing some modification to robertu idea if you are using crossplatform input into vector3 movement. I am using this on my project.

     public float h;                            //input axix x
     public float v;                            //input axis y
 
     private void FixedUpdate()
     {
             h = CrossPlatformInput$$anonymous$$anager.GetAxis(axisX);
             v = CrossPlatformInput$$anonymous$$anager.GetAxis(axisY);
 
        Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
 
         if (viewPos.y < 0.02f)
         {
             v = 1;
         }
        else if (viewPos.y > 0.99f)
        {
             v= -1;
        }
 
         if (viewPos.x < 0.01f)
         {
             h = 1;
         }
         else if (viewPos.x > 0.98f)
         {
             h = -1;
         }
 
         m_$$anonymous$$ove = v * Vector3.forward + h * Vector3.right;
 
 }


Follow this Question

Answers Answers and Comments

13 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

Related Questions

Maintain screenspace scale/position as camera field of view changes? 2 Answers

Distribute terrain in zones 3 Answers

3D Isometric Camera C# - Center on player? 2 Answers

UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers

Convert 2D Camera to 3D for my Gameplay UI Setup 0 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