Search Unity

C# Keep Gameobject within Bounds But Still Moving

Discussion in 'Scripting' started by kenaochreous, May 29, 2015.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    How do keep a gameobject within a boundary but still have it able to move? I tried using the if(boundary.Contains(go.transform.position)) to make sure that the gameobject wouldn't outside of the boundary. But this also caused the gameobject to stop moving once it had reached one of the ends of the boundary. Is there a way to adjust the parameters of Bounds.Contains or is there a more flexible if statement?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GO_Move : MonoBehaviour {
    5.     public Bounds boundary = new Bounds(Vector3.zero,new Vector3(5,5,0));
    6.     public GameObject go;
    7.     public string inputString;
    8.  
    9.     void LateUpdate ()
    10.     {
    11.         if(boundary.Contains(go.transform.position)){
    12.                 Vector3 movePositions = new Vector3(go.transform.position.x + Input.GetAxis("Horizontal"),go.transform.position.y + Input.GetAxis("Vertical"),go.transform.position.z);
    13.                 go.transform.position = Vector3.Lerp(go.transform.position, mousePositions, Time.smoothDeltaTime);
    14.         }
    15.     }
    16. }
     
    Last edited: May 29, 2015
  2. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    I dont shure what this what you need, but i use this code for camera to chek bound. And smooth return her when she outside bounds.

    Code (CSharp):
    1. // Check Left Map Bound
    2.         if (Target.transform.position.x < -(leftMaxBound + offsetBound))
    3.         {
    4.             Target.transform.position = Vector3.Lerp(
    5.                 Target.transform.position,
    6.                 new Vector3(leftMaxBound, 0, Target.transform.position.z),
    7.                 speedReturn * Time.deltaTime);
    8.         }
    9.         // Check Left Map Bound
    10.  
    11.  
    12.         // Check Down Map Bound
    13.         if (Target.transform.position.z < -(downMaxBound + offsetBound))
    14.         {
    15.             Target.transform.position = Vector3.Lerp(
    16.                 Target.transform.position,
    17.                 new Vector3(Target.transform.position.x, 0, downMaxBound),
    18.                 speedReturn * Time.deltaTime);
    19.         }
    20.         // Check Down Map Bound
    21.  
    22.  
    23.         // Check Right Map Bound
    24.         if (Target.transform.position.x > rightMaxBound + offsetBound)
    25.         {
    26.             Target.transform.position = Vector3.Lerp(
    27.                 Target.transform.position,
    28.                 new Vector3(rightMaxBound, 0, Target.transform.position.z),
    29.                 speedReturn * Time.deltaTime);
    30.         }
    31.         // Check Right Map Bound
    32.  
    33.  
    34.         // Check Up Map Bound
    35.         if (Target.transform.position.z > upMaxBound + offsetBound)
    36.         {
    37.             Target.transform.position = Vector3.Lerp(
    38.                 Target.transform.position,
    39.                 new Vector3(Target.transform.position.x, 0, upMaxBound),
    40.                 speedReturn * Time.deltaTime);
    41.         }
    42.         // Check Up Map Bound


    and not good idia use this

    Code (CSharp):
    1. if(boundary.Contains(go.transform.position))
    for checking distance, every time he never calculate true.

    use this for better comparsion distance between objects

    Code (CSharp):
    1.  
    2. if (Vector3.Distance(this.gameObject.transform.position, currentPos) < 0.05f)
    3. {
    4. }
    I hope you can easily modifity my code for your self
     
    Last edited: May 29, 2015
  3. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I'm sorry I don't quite understand. I'm not checking for distance between gameobjects. I just want to keep the gameobject within bounds but still allow it to move back.
     
  4. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    How about:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GO_Move : MonoBehaviour {
    5.   public Bounds boundary = new Bounds(Vector3.zero,new Vector3(5,5,0));
    6.   public GameObject go;
    7.   public string inputString;
    8.  
    9.   void LateUpdate ()
    10.   {
    11.     Vector3 movePositions = new Vector3(go.transform.position.x + Input.GetAxis("Horizontal"),RoundToNearestHalfSafer(go.transform.position.y + Input.GetAxis("Vertical")),go.transform.position.z);
    12.     if(boundary.Contains(movePositions)){
    13.       go.transform.position = Vector3.Lerp(go.transform.position, mousePositions, Time.smoothDeltaTime);
    14.     }
    15.   }
    16. }
     
  5. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    Nope same issue still, I just realized that I made couple of mistakes with code I uploaded in my first post. I should have just copy/paste my code instead of trying to remember what I wrote. Anyway here is the correct code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GO_Move : MonoBehaviour {
    5.     public Bounds boundary = new Bounds(Vector3.zero,new Vector3(5,5,0));
    6.     public GameObject go;
    7.     public string inputString;
    8.  
    9.     // Update is called once per frame
    10.     void LateUpdate ()
    11.     {
    12.      
    13.         if(inputString == "Keyboard"){
    14.             Vector3 movePositions = new Vector3(RoundToNearestHalfSafer(go.transform.position.x + Input.GetAxis("Horizontal")),RoundToNearestHalfSafer(go.transform.position.y + Input.GetAxis("Vertical")),go.transform.position.z);
    15.             if(boundary.Contains(go.transform.position)){
    16.                 go.transform.position = Vector3.Lerp(go.transform.position, movePositions, Time.smoothDeltaTime);
    17.             }
    18.         }
    19.     }
    20.     public static float RoundToNearestHalfSafer(float a)
    21.     {
    22.         return a = a - (a % 0.5f);
    23.     }
    24. }
    25.