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

Apply an Addforce type of effect to a Kinematic Rigidbody2D

Discussion in '2D' started by timdouglas90, Nov 25, 2015.

  1. timdouglas90

    timdouglas90

    Joined:
    Jun 3, 2014
    Posts:
    2
    Hey guys,

    So I have a doodle jump style game, I originally had the player bouncing off of the platforms, whilst the platforms themselves merely moved down the screen and got moved to the top of the screen. I've been told by folk that this would eat up a mobile device's memory a fair chunk so why not make it so the platforms have the addforce effect going down? Essentially making the play space not move.

    Here's my problem, I am moving the bounce platforms with a Kinematic Rigidbody 2D, and I can't do an addforce type of effect on them as they are already moving downwards...

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BouncePlatform : MonoBehaviour
    6. {  
    7.     public int jumpAmount;
    8.     public int moveSpeed;
    9.        
    10.     private Transform myTransform;
    11.     private BoxCollider2D platformBoxCollider;
    12.     private Player player;
    13.     private Rigidbody2D rb;
    14.      
    15.     void Start()  
    16.     {  
    17.         platformBoxCollider = GetComponent<BoxCollider2D>();
    18.         myTransform = this.transform;
    19.         rb = GetComponent<Rigidbody2D>();
    20.         player = FindObjectOfType<Player>();  
    21.     }  
    22.    
    23.     void Update()  
    24.     {
    25.         rb.velocity = Vector2.down;
    26.         if(player == null)
    27.         {
    28.             return;
    29.         }
    30.         else
    31.         {
    32.             if(player.transform.position.y > myTransform.transform.position.y)  
    33.             {      
    34.                 platformBoxCollider.enabled = true;  
    35.             }    
    36.             else  
    37.             {  
    38.                 platformBoxCollider.enabled = false;          
    39.             }
    40.         }  
    41.     }
    42.    
    43.     public void Jump()
    44.     {
    45.         //Addforce downward to the platforms
    46.     }
    47. }
    48.  
    And also here is my player script which is adding jumpforce as the rigidbody2D isn't kinematic...

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class Player : MonoBehaviour
    7. {      
    8.     public float moveFactor;
    9.     public int health;
    10.     public GameObject playerHealthText;
    11.     public bool isInvulnerable;
    12.  
    13.     private Animator animationController;
    14.     private Rigidbody2D rb;
    15.     private float invulnerableTimer;
    16.     private BouncePlatform bouncePlatform;
    17.    
    18.     public void Start()
    19.     {
    20.         invulnerableTimer = 5.0f;
    21.         animationController = GetComponent<Animator> ();
    22.         rb = GetComponent<Rigidbody2D>();
    23.         bouncePlatform = FindObjectOfType<BouncePlatform>();
    24.     }
    25.    
    26.     void Update()
    27.     {  
    28.         playerHealthText.GetComponent<Text> ().text = "Health: " + health.ToString();
    29.         health = Mathf.Clamp (health, 0, 3);
    30.  
    31.         if (health <= 0)
    32.         {
    33.             //TODO Some sort of level end thing, this also probably belongs in a GameManager object?
    34.             Destroy(gameObject);
    35.         }
    36.  
    37.         //invulnerability timer
    38.         if(isInvulnerable)
    39.         {
    40.             invulnerableTimer -= Time.deltaTime;
    41.             if(invulnerableTimer <= 0.0)
    42.             {
    43.                 isInvulnerable = false;
    44.             }
    45.         }
    46.  
    47.         //Camera things
    48.         OnBecameInvisible();
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         //Horizontal Movement
    54.         float playerXInput = Input.GetAxis("Horizontal");
    55.         float movePlayer = playerXInput * moveFactor;
    56.         rb.AddForce(new Vector2(movePlayer, 0f));
    57.     }
    58.  
    59.     void OnBecameInvisible()
    60.     {
    61.         var cam = Camera.main;
    62.         var viewportPosition = cam.WorldToViewportPoint(transform.position);
    63.         var newPosition = transform.position;
    64.  
    65.         if (viewportPosition.x > 1 || viewportPosition.x < 0)
    66.         {
    67.             newPosition.x = -newPosition.x;
    68.         }
    69.         transform.position = newPosition;
    70.     }
    71.    
    72.     void OnTriggerEnter2D(Collider2D collider)
    73.     {
    74.         if(collider.gameObject.tag == "BouncePlatform")
    75.         {
    76.             Debug.Log("Hit: " + collider.gameObject.tag);
    77.             bouncePlatform.Jump();
    78.             rb.AddForce(Vector2.up * 500);
    79.         }
    80.     }
    81.  
    82.     public void Damage(int damage)
    83.     {
    84.         if(!isInvulnerable)
    85.         {
    86.             animationController.SetTrigger ("damageTrigger");
    87.             health -= damage;
    88.         }
    89.         else
    90.         {
    91.             //TODO some sort of animation S***
    92.             return;
    93.         }
    94.     }
    95.    
    96.     public void Heal(int heal)
    97.     {
    98.         health += heal;
    99.     }
    100.    
    101.     public void IsInvulnerable(bool active)
    102.     {
    103.         isInvulnerable = active;
    104.     }
    105. }
    106.  
    Looking forward to getting schooled!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    If the body is Kinematic, why not just set its linear velocity directly using Rigidbody2D.velocity ?
     
    er1227 likes this.
  3. FenixShadow

    FenixShadow

    Joined:
    Dec 10, 2014
    Posts:
    3
    I have not had any luck directly manipulating a kinematic Rigidbody's velocity through script. The kinematic Rigidbody still doesn't move.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Kinematic bodies in 2D do move if you set their velocity directly so I'm not sure what's going on in your case.
     
  5. HikmetDuran

    HikmetDuran

    Joined:
    May 7, 2017
    Posts:
    11
    In my case this satisfied me just like AddForce()

    Just play with the values;

    !!Didn't tested for syntax errors but i hope you get the idea

    Code (CSharp):
    1.  
    2. float forceAmount = 5f;
    3.  
    4. IEnumerator FakeAddForceMotion()
    5. {
    6.     float i = 0.01f;
    7.     while (forceAmount > i)
    8.     {
    9.      _rigidBody2D.velocity = new Vector2(forceAmount / i, _rigidBody2D.velocity.y); // !! For X axis positive force
    10.      i = i + Time.deltaTime;
    11.      yield return new WaitForEndOfFrame();      
    12.     }
    13.     _rigidBody2D.velocity = Vector2.zero;
    14.     yield return null;
    15. }
    16.  
    17. // Call this when you want to apply force
    18. void AddForce()
    19. {
    20.     StartCoroutine(FakeAddForceMotion);
    21. }
     
    tinna01 likes this.
  6. Little_Narwhal_13

    Little_Narwhal_13

    Joined:
    Feb 6, 2017
    Posts:
    15
    I personally set a certain object to kinematic in the hope that addforce would indeed not affect it, but now it still gets flung away...
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    That makes no sense. Kinematic bodies do not react to forces. Can you demonstrate this happening on a Kinematic body?
     
    bastion5000 likes this.