Option for how to apply a force using Rigidbody2D.AddForce.
Use this to apply a certain type of force to a 2D RigidBody. There are two types of forces to apply: Force mode and Impulse Mode. For a 3D Rigidbody see ForceMode.
//This script adds force to a Rigidbody. The kind of force is determined by which buttons you click.
//Create a Sprite and attach a Rigidbody2D component to it //Attach this script to the Sprite
using UnityEngine; using UnityEngine.EventSystems;
public class AddingForce : MonoBehaviour { //Use to switch between Force Modes enum ModeSwitching { Start, Impulse, Force }; ModeSwitching m_ModeSwitching;
//Use this to change the different kinds of force ForceMode2D m_ForceMode; //Start position of the RigidBody, use to reset Vector2 m_StartPosition;
//Use to apply force to RigidBody Vector2 m_NewForce;
//Use to manipulate the RigidBody of a GameObject Rigidbody2D m_Rigidbody;
void Start() { //Fetch the RigidBody component attached to the GameObject m_Rigidbody = GetComponent<Rigidbody2D>(); //Start at first mode (nothing happening yet) m_ModeSwitching = ModeSwitching.Start;
//Initialising the force to use on the RigidBody in various ways m_NewForce = new Vector2(-5.0f, 1.0f);
//This is the RigidBody's starting position m_StartPosition = m_Rigidbody.transform.position; }
void Update() { //Switching modes depending on button presses switch (m_ModeSwitching) { //This is the starting mode which resets the GameObject case ModeSwitching.Start:
//Reset to starting position of RigidBody m_Rigidbody.transform.position = m_StartPosition; //Reset the velocity of the RigidBody m_Rigidbody.velocity = new Vector2(0f, 0f); break;
//This is the Force Mode case ModeSwitching.Force: //Make the GameObject travel upwards m_NewForce = new Vector2(0, 1.0f); //Use Force mode as force on the RigidBody m_Rigidbody.AddForce(m_NewForce, ForceMode2D.Force); break;
//This is the Impulse Mode case ModeSwitching.Impulse: //Make the GameObject travel upwards m_NewForce = new Vector2(0f, 1.0f); //Use Impulse mode as a force on the RigidBody m_Rigidbody.AddForce(m_NewForce, ForceMode2D.Impulse); break; } }
//These are the Buttons for telling what Force to apply as well as resetting void OnGUI() { //If reset button pressed if (GUI.Button(new Rect(100, 0, 150, 30), "Reset")) { //Switch to start/reset case
m_ModeSwitching = ModeSwitching.Start; }
//Impulse button pressed if (GUI.Button(new Rect(100, 60, 150, 30), "Apply Impulse")) { //Switch to Impulse mode (apply impulse forces to GameObject)
m_ModeSwitching = ModeSwitching.Impulse; }
//Force Button Pressed if (GUI.Button(new Rect(100, 90, 150, 30), "Apply Force")) { //Switch to Force mode (apply force to GameObject) m_ModeSwitching = ModeSwitching.Force; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.