Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Jonathan Genesis · Jan 24, 2014 at 01:56 AM · movementrigidbodycharacter

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");
     }
Comment
Add comment · Show 3
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 Exalia · Jan 24, 2014 at 02:01 AM 0
Share

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?

avatar image Jonathan Genesis · Jan 24, 2014 at 02:13 PM 1
Share

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

avatar image Exalia · Jan 24, 2014 at 02:18 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
7

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


Comment
Add comment · Show 5 · 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 Jonathan Genesis · Jan 24, 2014 at 10:15 PM 0
Share

where does this go in my code i put my code in the main thread. Does it go in the update function?

avatar image oatsbarley · Jan 25, 2014 at 12:00 AM 0
Share

This goes into the Update function of the object whose movement you're trying to constrain.

avatar image $$anonymous$$ · Nov 16, 2014 at 03:16 PM 1
Share

@oatsbarley typo...last variable says bottomRight.y, where it should say bottomLeft.y

avatar image oatsbarley · Nov 16, 2014 at 04:16 PM 0
Share

@$$anonymous$$ Good catch!

avatar image naviln · Jan 27, 2019 at 04:22 AM 0
Share

I used a variation of this with an offset. Thank you!

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

21 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

Related Questions

Creating a platform game on 2 axis. Should I use a rigid body, or a character controller. 1 Answer

Rigidbody Player Controller sticking to walls 0 Answers

Make characters go through each other 1 Answer

Velocity for movement 0 Answers

Tweaking character movement 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