Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Stop gameObject from pushing itself using isKinematic

Discussion in 'Physics' started by ctaminian, May 13, 2023.

  1. ctaminian

    ctaminian

    Joined:
    Aug 6, 2017
    Posts:
    1
    Hey everyone!

    How can I stop a gameobject from pushing a clone of itself around? I have a chicken that's being duplicated around the scene many times. They are all the same gameobject with a box collider, a rigidbody and the script you see bellow (Eventually I will convert to prefab).

    The game itself is simple...some of the chickens can only move horizontally, others only vertically based on the "swipe" direction...which I'm testing now with mouseDown and mouseUp.

    I have tried the following, but i'm stuck:

    - On start I set the chicken's rigidbody to isKinematic
    - Then when I click the mouse on it, I set isKinematic to false (so it can move)
    - Then onCollisionEnter if I hit the "Chicken", I set isKinematic to true (so it can't be pushed anymore)
    - Now when I click again on it, (it should set the isKinematic to false, so it can move again) but it's stuck and can't figure out how to reset the isKinematic again...

    I would appreciate your help! Thank you!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MoveChicken : MonoBehaviour
    7. {
    8.     Vector3 clickInitPos;
    9.     Vector3 clickEndPos;
    10.     Vector3 forceDirection;
    11.  
    12.     bool applyForce = false;
    13.     float forceStrength = 130f;
    14.  
    15.     [SerializeField] bool canMoveHorizontal;
    16.     [SerializeField] bool canMoveVertical;
    17.  
    18.     Rigidbody rb;
    19.     Animator anim;
    20.    
    21.     void Start()
    22.     {
    23.         rb = GetComponent<Rigidbody>();
    24.         anim = GetComponent<Animator>();
    25.         rb.isKinematic = true;
    26.     }
    27.     void OnMouseDown()
    28.     {
    29.         CalculateClickStartPosition();
    30.     }
    31.  
    32.     void OnMouseUp()
    33.     {
    34.         CalculateClickEndPosition();
    35.     }
    36.  
    37.     void FixedUpdate()
    38.     {
    39.         MoveTheChicken();
    40.     }
    41.  
    42.     void OnCollisionEnter(Collision collision)
    43.     {
    44.         ResetChicken();
    45.         if (collision.gameObject.tag == "Chicken")
    46.         {
    47.             rb.isKinematic = true;
    48.             Debug.Log("I'm Stuck Here");
    49.         }
    50.     }
    51.  
    52.     void OnCollisionStay(Collision collision)
    53.     {
    54.         ResetChicken();
    55.     }
    56.  
    57.     void CalculateClickStartPosition()
    58.     {
    59.         if (canMoveHorizontal)
    60.         {
    61.             clickInitPos = new Vector3(Input.mousePosition.x, 0, 0);
    62.         }
    63.  
    64.         if (canMoveVertical)
    65.         {
    66.             clickInitPos = new Vector3(0, Input.mousePosition.y, 0);
    67.         }
    68.        
    69.         rb.isKinematic = false;
    70.         rb.WakeUp();
    71.     }
    72.  
    73.     void CalculateClickEndPosition()
    74.     {
    75.         if (canMoveHorizontal)
    76.         {
    77.             clickEndPos = new Vector3(Input.mousePosition.x, 0, 0);
    78.         }
    79.  
    80.         if (canMoveVertical)
    81.         {
    82.             clickEndPos = new Vector3(0, Input.mousePosition.y, 0);
    83.         }
    84.  
    85.         forceDirection = clickEndPos - clickInitPos;
    86.         forceDirection = forceDirection.normalized;
    87.  
    88.         if (forceDirection != Vector3.zero)
    89.         {
    90.             applyForce = true;
    91.             anim.SetBool("Run", true);
    92.         }
    93.     }
    94.  
    95.     void MoveTheChicken()
    96.     {
    97.         if (applyForce)
    98.         {
    99.             if (canMoveHorizontal)
    100.             {
    101.                 rb.AddForce(forceDirection.x * forceStrength, 0, 0);
    102.                 FaceDirection();
    103.             }
    104.  
    105.             if (canMoveVertical)
    106.             {
    107.                 rb.AddForce(0, 0, forceDirection.y * forceStrength);
    108.                 FaceDirection();
    109.             }
    110.  
    111.             applyForce = false;
    112.         }
    113.  
    114.     }
    115.  
    116.     void ResetChicken()
    117.     {
    118.         applyForce = false;
    119.         anim.SetBool("Run", false);
    120.     }
    121.  
    122.     void FaceDirection()
    123.     {
    124.         if (forceDirection.x > 0)
    125.         {
    126.             transform.localRotation = Quaternion.Euler(0, 90f, 0);
    127.         }
    128.  
    129.         if (forceDirection.x < 0)
    130.         {
    131.             transform.localRotation = Quaternion.Euler(0, -90f, 0);
    132.         }
    133.  
    134.         if (forceDirection.y > 0)
    135.         {
    136.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    137.         }
    138.  
    139.         if (forceDirection.y < 0)
    140.         {
    141.             transform.localRotation = Quaternion.Euler(0, 180f, 0);
    142.         }
    143.     }
    144. }
    145.  
     

    Attached Files: