• 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
Question by animeman0 · Aug 19, 2015 at 11:18 AM · c#camera2dgameobject

Gameobject to stay within the view of the camera - c#

I'm trying to make a top down survival game but I need to make the play stay within the view of the camera, and say if he got to the edge he would stop.

Thanks in advance.

Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image

Answer by Priyanshu · Aug 19, 2015 at 11:26 AM

This live training session from Unity will help you with your current problem and maybe more regarding Top Down Shmup.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/space-shooter-to-mobile

Comment

People who like this

0 Show 0 · 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

Answer by jakhir1 · Aug 19, 2015 at 11:30 AM

What you could do is get the coordinates for the edge of the screen, so move the player to the edge and get his X position or whatever you're using and put something like this

 //7 = edge of the screen
 if(transform.position.x >= 7)
 {
   transform.position.x = 7;
 }
 else if(transform.position.x <= -7)
 {
   transform.position.x = -7;
 }

You should still be able to move the player between these two values. I can't imagine what your game is going to look like so just give this a shot.

Comment
animeman0

People who like this

1 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 animeman0 · Aug 19, 2015 at 12:15 PM 0
Share

I tried what you said an it gives me this

Assets/Scripts/PlayerMovement.cs(39,30): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

avatar image Priyanshu · Aug 19, 2015 at 12:19 PM 0
Share

Instead of

transform.position.x = 7;

use

transform.position = new Vector2( 7, transform.position.y);

avatar image animeman0 · Aug 20, 2015 at 10:38 AM 0
Share

Thanks it worked!

avatar image

Answer by Driseus · Aug 19, 2015 at 12:18 PM

Use the Camera frustum its explained here: http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

2D C# destroy a GameObject on collision 2 Answers

Cinemachine camera rotating with player 2D 1 Answer

Stop 2D camera X axis movement if equal to one of two coordinates 1 Answer

weird spawning issue - Unity 2D 1 Answer

how are Mathf.SmoothDamp and Mathf.SmoothStep different 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