- Home /
How to add forward velocity to a Rigidbody2D?
I can't seem to figure out how to do this. I've checked the documentation, but it doesn't really explain how to do it. This is the script I'm using right now:
var speed : int = 10;
function Update ()
{
rigidbody2D.AddForce(transform.forward * speed * Time.deltaTime);
}
It seems like it should be pretty simple. With this code, I'm not getting an error, but the object isn't moving at all when it spawns. I've also tried to capitalize Rigidbody2D, 'cause I'm a little unclear on how it is supposed to be capitalized, but when I do that it gives me this error:
An instance of type 'UnityEngine.Rigidbody2D' is required to access non static member 'AddForce'.
So I'm pretty sure the one without capitalization is correct. But it still isn't moving? Could anybody help me understand why it isn't moving and how I should go about fixing it? Thanks in advance for anybody's help.
Capitalizing Rigidbody2D would mean calling a static class function, but as you want the rigidbody of our GameObject to move, you want to call its instance, so the code should be fine. Do you get any runtime errors? Otherwise try with a bigger speed value, depending on the mass and drag of your rigidbody object.
Answer by azmat786n · Dec 26, 2013 at 04:25 PM
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 10;
public float jumpForce = 100;
public bool isGrounded = true;
public float old_y;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(transform.position.y <=old_y+0.1f && !isGrounded) {
isGrounded = true;
}
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)) {
transform.Translate(Input.GetAxis("Horizontal")*moveSpeed, 0,0);
}
if(isGrounded && Input.GetKey(KeyCode.Space)) {
isGrounded = false;
old_y = transform.position.y;
rigidbody2D.AddForce(Vector2.up * jumpForce);
}
}
void OnColissionEnter(Collision col) {
if(col.collider.tag == "ground")
isGrounded = true;
}
}
Alrighty, this is what ended up helping me so ima mark it as the answer. I took this line:
transform.Translate(Input.GetAxis("Horizontal")*moveSpeed, 0,0);
and turned it into this line:
transform.Translate(moveSpeed, 0,0);
and stuck that in my update function. Worked perfectly!
var speed : float = 10;
function Update ()
{
rigidbody2D.AddForce(transform.forward * speed * Time.deltaTime);
}
//you can use your own script by some changes like
var speed : int = 10;
function Update ()
{
rigidbody2D.AddForce(Vector2.right * speed * Time.deltaTime);
}
//this code is not tested but it should work...
Answer by robertbu · Dec 26, 2013 at 04:29 PM
The likely problem is that you are using 'transform.forward'. Rigidbody2D works on the XY plane, and typically you would be using a sprite for your projectile. So you want to use 'transform.up' or 'transform.right' depending on how you've setup your game and your sprite. In addition, 'speed' should be a float and likely have a larger value than '10'. For an object with a mass of 1.0, start with 500.
Thank you very much for your help, yours was a good answer too. But alas, I can only mark one answer as correct. +1 for you, good sir.