x


Player controller

how can i say to the player that don't get out of the screen(camera field of view)? my player and camera always are moving forward and when i controlling the player,it's get out of screen.

The genre of game is top down plane shooter. My camera and player are moving forward with a constant speed. Also player can controll the character and move it. I want to say to player that dont get out of the screen. If i attach the character to camera,then if i move the character in axis of left and right,camera will move too. But i dont want to .also my game is not top down completely. It's a bit perspective. The script code for player is below:

using Unit

yEngine; using System.Collections;
public class Plane_Controller : MonoBehaviour {
// Use this for initialization void Start () { } float x_position=0;
float z_position=0;
public Rigidbody Bullet;
public int i=5;
#endregionc
//----Set to the Plane----
// Update is called once per frame
void Update ()
{
transform.Translate (0,0,10*Time.deltaTime);
#region positioning
//----D----

if(Input.GetKey(KeyCode.D))
{
if (this.transform.position.x < GameObject.FindGameObjectWithTag("MainCamera").transform.position.x+40 ){
x_position=1;
transform.position+=
new Vector3(x_position*Time.deltaTime*8,0,0);
//Rotate Right
transform.localRotation = Quaternion.Euler(0,0,-30);
}
}
//----A----
if(Input.GetKey(KeyCode.A))
{
if (this.transform.position.x > GameObject.FindGameObjectWithTag("MainCamera").transform.position.x-40 ){
x_position=1;
transform.position-= new Vector3(x_position*Time.deltaTime*8,0,0);
//Rotate Left
transform.localRotation = Quaternion.Euler(0,0,30); 
}
}
//----S----
if(Input.GetKey(KeyCode.S))
{
if (this.transform.position.z > GameObject.FindGameObjectWithTag("MainCamera").transform.position.z-20 ){
z_position=1;
transform.position-=
new Vector3(0,0,z_position*Time.deltaTime*8);
}
}
//----W----
if(Input.GetKey(KeyCode.W))
{
if (this.transform.position.z < GameObject.FindGameObjectWithTag("MainCamera").transform.position.z+20 ){
z_position=1;
transform.position+=
new Vector3(0,0,z_position*Time.deltaTime*8);
}
}
//----Rotate default----
if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp (KeyCode.D)){
transform.localRotation = Quaternion.Euler(0,0,0);
} 
this.transform.position = new Vector3(this.transform.position.x,13,this.transform.position.z);
#endregion

In other script i say to camera that move forward like player in default. The upper code is not good for my game. Because my game is perspective and upper's side of screen is my problem. I want a script that can recognize the field of view and force the player to move only inside the field of view.

more ▼

asked Aug 09 '12 at 08:00 AM

karafs gravatar image

karafs
24 2 5

I wonder if it's possible to write an AI system that is better at formatted code on this forum than new users are at formatting code on this forum?

Aug 09 '12 at 08:20 AM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Ok,

Give the input to your ship as normal. In the same update check if the position is more or less than the camera position +/- the distance to the side of the screen.

var minPos = Camera.main.transform.position.x - difference;
var maxPos = Camera.main.transform.position.x + difference;
if(transform.position.x<minPos)transform.position.x=minPos;
if(transform.position.x>maxPos)transform.position.x=maxPos;
more ▼

answered Aug 09 '12 at 08:03 AM

fafase gravatar image

fafase
10.5k 9 15 40

In the first line you intialized a variable in java. How can i write this code in c#?what's the variable type?

Aug 09 '12 at 08:54 AM karafs
public float minPos;
Aug 09 '12 at 08:57 AM fafase
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2983
x519

asked: Aug 09 '12 at 08:00 AM

Seen: 316 times

Last Updated: Aug 09 '12 at 08:57 AM