- Home /
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.
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.
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
Ins$$anonymous$$d of
transform.position.x = 7;
use
transform.position = new Vector2( 7, transform.position.y);
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
Answer by Driseus · Aug 19, 2015 at 12:18 PM
Use the Camera frustum its explained here: http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html