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

How to Make a RigidBody not accelerate?

Discussion in '2D' started by DischordDynne, Mar 7, 2021.

  1. DischordDynne

    DischordDynne

    Joined:
    Feb 20, 2021
    Posts:
    2
    I'm trying to make a moving platform that slides up and down. I'm new to Unity so this might not be the best way to do this. I need to make the platform not eccelerate so it doesn't fly off the screen. Does anyone know a better way to do this or a way to stop acceleration?

    Here is my Code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MovingPlatforms : MonoBehaviour
    4. {
    5.     private Vector3 lowestPos;
    6.  
    7.  
    8.  
    9.     private void Start()
    10.     {
    11.         lowestPos = transform.position;
    12.     }
    13.  
    14.  
    15.     private void Update()
    16.     {
    17.      
    18.         if (transform.position.y >= lowestPos.y + 3)
    19.         {
    20.             GetComponent<Rigidbody2D>().gravityScale = 1;
    21.         }
    22.         if (transform.position.y <= lowestPos.y)
    23.         {
    24.             GetComponent<Rigidbody2D>().gravityScale = -1;
    25.         }
    26.  
    27.     }
    28.  
    29.  
    30.  
    31. }
     
    Last edited: Mar 8, 2021
  2. DischordDynne

    DischordDynne

    Joined:
    Feb 20, 2021
    Posts:
    2
    I realized that this plan wouldn't work and that when another Rigidbody lands on top, It falls down due to the gravity increase. So instead of using gravity does anyone have a better way of doing this?