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 MalzanFaren · Sep 07, 2015 at 02:44 PM · movementboundary

Object gets stuck outside boundary if pushed

Hi, I'm writing a script which controls an object which moves randomly around the 2D play area. The idea is that the game ends if the player runs into it, but for now I'm having a little trouble with the movement. I've used a random movement script (perhaps not the best one) which selects random x and y components for a vector and then uses that to alter the Transform. I've also used Mathf.Clamp to stop it leaving the boundary area alongside a little code to turn it when it reaches the boundary and gets it out of corners, both of which are really just there to keep it moving smoothly. Finally, I've added a boolean loop which causes it to head towards the player if the player gets too close (this doesn't need to be in a boolean at the moment but I was thinking of adding some things which would use it and I don't think it's the problem in any case).

This works to a certain extent, but I've found that the player can push the object out of the boundary, at which point it's then stuck outside/at the boundary. Technically this isn't a problem at the moment as I'm going to code in a collision with the player ending the game, but I'd whether fix it now rather than come across it again if I add another feature.

I've done some checking and removing all the boundary related scripts (detect corner, reverse direction at boundary, and mathf.clamp) fixes the problem, but removing only 1 of these doesn't so I can't say for certain the exact issue. Any help would be appreciated. Apologies for the long code, I'm not 100% certain the problem so I thought it best to include the whole thing.

using UnityEngine; using System.Collections;

 public class BabyControllerScript : MonoBehaviour {
     
     private float tChange =0;
     private float randomX;
     private float randomY;
     private float RandomTurn;
     public float moveSpeed;
     public Boundary boundary;
     public float AllowedTurn;
     private Quaternion Turn;
     
     public GameObject player;
     private Vector2 PlayerPositionX;
     private Vector2 StartDirection;
     private Vector2 FinalDirection;
     
     //for coerner logic
     private bool InCorner;
     public float CornerLimitX;
     public float CornerLimitY;
     private float RanX1;
     private float RanX2;
     private float RanY1;
     private float RanY2;
     private float TempRanX;
     private float TempRanY;
     private float EndCornerExit;
     
     //for too close feature
     public float TooClose;
     public float TooCloseTime;
     private float EndTooClose;
     
     //for move towards cat feature
     private bool CloseToPlayer;
     public float ClosePlayerDist;
     
     // Use this for initialization
     void Start () {
         InCorner = false;
         CloseToPlayer = false;
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         
         // loop used for moving towards player if too close
         if ((player.GetComponent<Rigidbody2D> ().position - GetComponent<Rigidbody2D> ().position).magnitude <= ClosePlayerDist) {
             CloseToPlayer = true;
         } else {
             CloseToPlayer = false;
         }
         
         
         if ( CloseToPlayer == true){
             FinalDirection = player.GetComponent<Rigidbody2D> ().position - GetComponent<Rigidbody2D> ().position;
             //performs the actual movement
             transform.Translate (FinalDirection.normalized * moveSpeed * Time.fixedDeltaTime);
             //Makes sure the object doesn't leave the borders
             GetComponent<Rigidbody2D> ().position = new Vector2
                 (
                     Mathf.Clamp (GetComponent<Rigidbody2D> ().position.x, boundary.minX, boundary.maxX),
                     Mathf.Clamp (GetComponent<Rigidbody2D> ().position.y, boundary.minY, boundary.maxY)
                     );
         } 
         //loop used for changing to random direction at random intervals
         else {
             if (Time.time >= tChange) {
                 
                 //prevents corner exit loop
                 if (Time.time >= EndCornerExit) {
                     InCorner = false;
                 }
                 //Direction select loop
                 if (InCorner == true) {
                     TempRanX = Random.Range (RanX1, RanX2);
                     TempRanY = Random.Range (RanY1, RanY2);
                     FinalDirection = new Vector2 (TempRanX, TempRanY);
                 } else {
                     TempRanX = Random.Range (-1.0f, 1.0f);
                     TempRanY = Random.Range (-1.0f, 1.0f);
                     FinalDirection = new Vector2 (TempRanX, TempRanY);
                 }
                 
                 // set a random interval between 0.5 and 1.5
                 tChange = Time.time + Random.Range (0.25f, 1.0f);
             }
             
             
             //performs the actual movement
             transform.Translate (FinalDirection.normalized * moveSpeed * Time.fixedDeltaTime);
             
             //reverts direction because it reached a border
             if (transform.position.x >= boundary.maxX || transform.position.x <= boundary.minX) {
                 FinalDirection.x = -FinalDirection.x;
             }
             if (transform.position.y >= boundary.maxY || transform.position.y <= boundary.minY) {
                 FinalDirection.y = -FinalDirection.y;
             }
             
             //Checks whether to activate corner exit loop
             if (boundary.maxX - transform.position.x <= CornerLimitX) {
                 if (boundary.maxY - transform.position.y <= CornerLimitY) {
                     //+ve x +ve y
                     InCorner = true;
                     RanX1 = -1.0f;
                     RanX2 = 0.0f;
                     RanY1 = -1.0f;
                     RanY2 = 0.0f;
                     EndCornerExit = Time.time + Random.Range (2.0f, 3.0f);
                 } else if (-boundary.maxY - transform.position.y >= CornerLimitY) {
                     //+ve x -ve y
                     InCorner = true;
                     RanX1 = -1.0f;
                     RanX2 = 0.0f;
                     RanY1 = 0.0f;
                     RanY2 = 1.0f;
                 }
             }
             
             if (-boundary.maxX - transform.position.x >= CornerLimitX) {
                 if (boundary.maxY - transform.position.y <= CornerLimitY) {
                     //-ve x +ve y
                     InCorner = true;
                     RanX1 = 0.0f;
                     RanX2 = 1.0f;
                     RanY1 = -1.0f;
                     RanY2 = 0.0f;
                     EndCornerExit = Time.time + Random.Range (2.0f, 3.0f);
                 } 
                 else if (-boundary.maxY - transform.position.y >= CornerLimitY) {
                     //-ve x -ve y
                     InCorner = true;
                     RanX1 = 0.0f;
                     RanX2 = 1.0f;
                     RanY1 = 0.0f;
                     RanY2 = 1.0f;
                 }
                 
             }
             
             //Makes sure the Object doesn't leave the borders
             GetComponent<Rigidbody2D> ().position = new Vector2
                 (
                     Mathf.Clamp (GetComponent<Rigidbody2D> ().position.x, boundary.minX, boundary.maxX),
                     Mathf.Clamp (GetComponent<Rigidbody2D> ().position.y, boundary.minY, boundary.maxY)
                     );
         }
     }
 }



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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Clamping movement range on X-axis 0 Answers

Restric the script and movement in specific area 0 Answers

Little problem with Mathf.Clamp as used in Space Shooter Tutorial 8 Answers

Setting boundary for Z axis 1 Answer

Restrict character's movement within a level's map 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