- Home /
The question is answered, right answer was accepted
I need help in using Impulse Force (2D)
I am a beginner at Unity, so I am just trying to do some basic stuff. I wanted to try to make a bar move horizontally in 2D mode, and I did it. Then I noticed it was taking time to start moving,accelerate, and change direction because it follows force = mass x acceleration, but I don't want that. With the docs and scripting API, I found that I should use a 2D Impulse force to solve my problem. I just don't know how to fix my current code to use the impulse force instead, even though I have tried it many times in different ways. This is the code I am using:
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour {
public float speed;
private Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
Vector2 movement = new Vector2 (moveHorizontal, 0);
rb.AddForce (movement * speed);
}
}
Answer by meat5000 · Jul 01, 2015 at 07:02 PM
Ok I lied. It seems ForceMode has indeed been added to Rigidbody2D.
http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
rb.AddForce(Vector2(x,y),ForceMode2D.Impulse);
Follow this Question
Related Questions
My Rope 2D System behaves abnormally 2 Answers
How to avoid Rigidbody collider from getting stuck between walls 1 Answer
Rigidbody2D.addforce giving jerky movevent 0 Answers
Is calling OnCollisionEnter2D on allot of GameObjects bad for performance? 2 Answers
Problem with 2D collision detection (Wide object falls through a thin hole) 1 Answer