Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I need some help with physics problem C#

Discussion in 'Scripting' started by ardizzle, Aug 17, 2014.

  1. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I have a game with some moving rectangles. They move around at random in a set space and when they collide with something they stop moving. The problem I have though is after they collide with something they sometimes move in that same direction again but since they already collided and haven't left each other then they don't get called to stop and they just push at each other forever. I know I could use OnCollisionStay but that seems pretty resource heavy especially if I have 10 to 20 enemies moving around at the same time.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    just change the rigidbody to iskinematic on collision
     
  3. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    use is kinetic ticked
     
  4. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    When I do that they push each other out of the set range and thru walls. Ill post my current code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class AIMovement : MonoBehaviour
    6. {
    7.     public float minMoveDelay = 3f;
    8.     public float maxMoveDelay = 6f;
    9.     public float speed = 2f;
    10.     public AddOptions extraOptions;
    11.     // private vars
    12.     //The area the enemy can move in
    13.     private float minX = 4.4f;
    14.     private float maxX = -4.5f;
    15.     private float minY = 4.5f;
    16.     private float maxY = -2.5f;
    17.     private Vector2 newPos;
    18.     private bool moving = true;
    19.     private Vector3 startPos;
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         FindPlace();
    24.         startPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.         if(moving == true && extraOptions.doesMove == true)
    31.         {
    32.             Move();
    33.         }
    34.         if(extraOptions.avoidBall == true)
    35.         {
    36.             AvoidBall();
    37.         }
    38.     }
    39.    
    40.     void AvoidBall()
    41.     {
    42.         // Slowly moves away from the ball  
    43.     }
    44.  
    45.     void FindPlace()
    46.     {
    47.         // Search for position to move to
    48.         newPos.Set(Random.Range(minX, maxX), Random.Range(minY, maxY));
    49.     }
    50.    
    51.     void Move()
    52.     {
    53.         Vector3 temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
    54.         // Moves the brick in the direction of the transform
    55.         if(extraOptions.lockXAxis == false)
    56.         {
    57.             if(gameObject.transform.localPosition.x < newPos.x)
    58.             {
    59.                 temp = new Vector3(gameObject.transform.position.x + (speed/100), gameObject.transform.position.y, gameObject.transform.position.z);
    60.                 gameObject.transform.position = temp;
    61.             }
    62.             if(gameObject.transform.localPosition.x > newPos.x)
    63.             {
    64.                 temp = new Vector3(gameObject.transform.position.x - (speed/100), gameObject.transform.position.y, gameObject.transform.position.z);
    65.                 gameObject.transform.position = temp;
    66.             }
    67.         }
    68.         else
    69.         {
    70.             startPos = new Vector3(startPos.x, gameObject.transform.position.y, gameObject.transform.position.z);
    71.         }
    72.         if(extraOptions.lockYAxis == false)
    73.         {
    74.             if(gameObject.transform.localPosition.y < newPos.y)
    75.             {
    76.                 temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + (speed/100), gameObject.transform.position.z);
    77.                 gameObject.transform.position = temp;
    78.             }
    79.             if(gameObject.transform.localPosition.y > newPos.y)
    80.             {
    81.                 temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + (speed/100), gameObject.transform.position.z);
    82.                 gameObject.transform.position = temp;
    83.             }
    84.         }
    85.         else
    86.         {
    87.             startPos = new Vector3(gameObject.transform.position.x, startPos.y, gameObject.transform.position.z);
    88.         }
    89.         if(gameObject.transform.localPosition.x <= newPos.x + .25 && gameObject.transform.localPosition.x >= newPos.x - .25 || extraOptions.lockXAxis == true)
    90.         {
    91.             if(gameObject.transform.localPosition.y <= newPos.y + .25 && gameObject.transform.localPosition.y >= newPos.y - .25 || extraOptions.lockYAxis == true)
    92.             {
    93.                 if(extraOptions.lockXAxis == false || extraOptions.lockYAxis == false)
    94.                 {
    95.                     Stopped();
    96.                 }
    97.             }
    98.         }
    99.     }
    100.    
    101.     void Stopped()
    102.     {
    103.         // Arived at target or hit something that stopped it
    104.         StartCoroutine(MovementDelay());
    105.     }
    106.    
    107.     IEnumerator MovementDelay()
    108.     {
    109.         // Delays the movement for X ammount of time
    110.         moving = false;
    111.         FindPlace();
    112.         yield return new WaitForSeconds(Random.Range(minMoveDelay, maxMoveDelay));
    113.         moving = true;
    114.     }
    115.    
    116.     void OnCollisionEnter2D(Collision2D coll)
    117.     {
    118.         if(coll.gameObject.tag != "Ball" || extraOptions.stopIfHit == true)
    119.         {
    120.             Stopped();
    121.         }
    122.     }
    123.     [System.Serializable]
    124.     public class AddOptions
    125.     {
    126.         public bool lockYAxis = false;
    127.         public bool lockXAxis = false;
    128.         public bool avoidBall = false;
    129.         public bool doesMove  = true;
    130.         public bool stopIfHit = true;
    131.     }
    132. }
    133.  
     
  5. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    check the tag for gm ball
     
  6. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I don't think you under stood what i was saying. I need the objects to know when they are touching another object not just when they colide.
     
  7. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    you may use contact point
     
  8. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    on OnCollisionEnter
     
  9. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I see use contactPoint to get the collider and then compare where each other is at in world space and move the opposite way. Thanks :)
     
  10. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    u r welcome