- Home /
How to keep object from going off screen
var moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"), 0);
rigidbody.MovePosition(transform.position + moveDirection*moveSpeed*Time.deltaTime);
here is my movement script and i want to make it so the character can ply move if he is within the bounds of the screen.
#pragma strict
var moveSpeed : float = 1.0;
var Health : int = 20;
private var Ammo : GameObject;
var bullet : GameObject;
var bomb : GameObject;
var misile : GameObject;
var sheild : GameObject;
var PowerParticles : GameObject[];
var bulletSpeed : float = 1.0;
var shootDelay : float = 0.2;
public var Fire : AudioClip;
public var Hurt : AudioClip;
public var Power : AudioClip;
var HealthText : GameObject;
var number : int = 1;
private var canShoot : boolean = true;
private var i : int = 0;
private var e : int = 0;
function Start(){
Ammo = bullet;
}
function FixedUpdate (){
HealthText.GetComponent(TextMesh).text = (Health.ToString());
if (Health <= 0){
Playerdie();
}
var moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"), 0);
rigidbody.MovePosition(transform.position + moveDirection*moveSpeed*Time.deltaTime);
if ((Input.GetAxis("FireHorizontal") != 0.0f || Input.GetAxis("FireVertical") != 0.0f) && canShoot){
audio.PlayClipAtPoint(Fire, Vector3 (0, 0, 0));
var shootDirection = Vector3(Input.GetAxis("FireHorizontal"),Input.GetAxis("FireVertical"), 0).normalized;
var AmmoInstance = Instantiate(Ammo,transform.position,Quaternion.LookRotation(shootDirection));
Destroy (AmmoInstance, 10);
AmmoInstance.rigidbody.AddForce(shootDirection*bulletSpeed,ForceMode.VelocityChange);
canShoot = false;
Invoke("ShootDelay", shootDelay);
}
}
function OnCollisionEnter(collision : Collision) {
rigidbody.velocity = Vector3.zero;
if (collision.gameObject.tag == "Enemy"){
Damage();
}
if (collision.gameObject.tag == "Health"){
var Healthp = Instantiate(PowerParticles[0], transform.position, transform.rotation);
Destroy (Healthp, 1);
audio.PlayClipAtPoint(Power, Vector3 (0, 0, 0));
Heal();
Destroy(collision.gameObject,0);
}
if (collision.gameObject.tag == "Machinegun"){
var MachineP = Instantiate(PowerParticles[1], transform.position, transform.rotation);
Destroy (MachineP, 1);
audio.PlayClipAtPoint(Power, Vector3 (0, 0, 0));
Destroy(collision.gameObject,0);
shootDelay = 0;
yield WaitForSeconds(1);
shootDelay = 0.2;
}
if (collision.gameObject.tag == "Bomb"){
var Bombp = Instantiate(PowerParticles[2], transform.position, transform.rotation);
Destroy (Bombp, 1);
audio.PlayClipAtPoint(Power, Vector3 (0, 0, 0));
Destroy(collision.gameObject,0);
Ammo = bomb;
yield WaitForSeconds(1);
Ammo = bullet;
}
if (collision.gameObject.tag == "Seekingmissile"){
var Missilep = Instantiate(PowerParticles[3], transform.position, transform.rotation);
Destroy (Missilep, 1);
audio.PlayClipAtPoint(Power, Vector3 (0, 0, 0));
Destroy(collision.gameObject,0);
Ammo = misile;
yield WaitForSeconds(1);
Ammo = bullet;
}
if (collision.gameObject.tag == "Shield"){
audio.PlayClipAtPoint(Power, Vector3 (0, 0, 0));
Destroy(collision.gameObject,0);
Shield();
}
}
function Shield(){
var currentshield = Instantiate(sheild, transform.position, transform.rotation);
currentshield.transform.parent = transform;
Destroy (currentshield, 1.5);
}
function OnCollisionExit(collision : Collision){
rigidbody.velocity = Vector3.zero;
}
function ShootDelay(){
canShoot = true;
}
function Damage(){
if (Health > 0){
audio.PlayClipAtPoint(Hurt, Vector3 (0, 0, 0));
Health--;
}
}
function Heal(){
if (Health < 40){
Health++;
}
}
function Playerdie(){
yield WaitForSeconds(1);
Application.LoadLevel("menu");
}
ply move? pnly?
If you're using an orthographic camera this is very easy to do.
If you're using a perspective camera, not so much
Please provide some more information.
Do you want the player to stop moving if it is outside of the camera view? or do you want the player to not be able to exit the camera view? Let us know what camera you are using Orthographic or Perspective. IS this game 2D or 3D?
I'm using an orthographic camera and I want to make it so the player cannot go outside of the screen I'm using 3D meshes and colliders ( not using new 2D toolkit) But the game is a 2D top down this is just the player movement controll
Awesome, then there are a few ways you can do this.
If your camera is static :
Set up 4 box colliders around the outside of your orthographic camera's view.
If your camera is dynamic but transformation is static on at least 2 axis :
Set up 4 box colliders that are parented to the cameras position.
If your camera is dynamic in every way :
Set up 4 box colliders and parent everything to your camera.
If this isn't sufficient let me know and I can help find out another solution :)
Answer by oatsbarley · Jan 24, 2014 at 02:40 PM
You can clamp the object's position using Mathf.Clamp():
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, cameraRect .xMin, cameraRect .xMax),
Mathf.Clamp(transform.position.y, cameraRect .yMin, cameraRect .yMax),
transform.position.z);
You could then check the clamped position against the position before it was clamped and play a pushing animation or something.
Getting the world Rect of a Camera is done like so:
var bottomLeft = camera.ScreenToWorldPoint(Vector3.zero);
var topRight = camera.ScreenToWorldPoint(new Vector3(
camera.pixelWidth, camera.pixelHeight));
var cameraRect = new Rect(
bottomLeft.x,
bottomLeft.y,
topRight.x - bottomLeft.x,
topRight.y - bottomLeft.y);
where does this go in my code i put my code in the main thread. Does it go in the update function?
This goes into the Update function of the object whose movement you're trying to constrain.
@oatsbarley typo...last variable says bottomRight.y, where it should say bottomLeft.y