Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

I tried what you said an it gives me this

Assets/Scripts/Player$$anonymous$$ovement.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

Ins$$anonymous$$d 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
0

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

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

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

28 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

Related Questions

How to detect an object which be in FOV of certain camera ? 1 Answer

2d game sidescroller camera UNITY 5 1 Answer

The New 2D Pixel Perfect Camera Package "Stretch Fill" Causes Major Lag 0 Answers

Why are my 2D sprite GameObjects blinking and how do I fix this problem? 0 Answers

How to scroll progressively a game object? 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