- Home /
2D C# Jump Script Addforce
Hey, im new to unity and scripting in C# and I need a bit of advice on scripting. I am trying to make my player jump when he hits W and is touching the ground. I Have added a rigidbody2d and and I have created a simple ground checker and when I use
Transform.translate
(Not the exact script just off memory) but it is very choppy, so I am trying to use this
rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode.Acceleration);
but its not working. It gives me an error: Addforce takes two arguments Sorry if I'm unclear thanks for your time. If you need more information of you don't understand what I'm saying just say something and I'll try to fix it.
Full script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpSpeed = 10f;
void Update()
{
//Next two if statements are for moving left and right
if (Input.GetKey (KeyCode.D))
transform.Translate (new Vector2 (1, 0) * moveSpeed * Time.deltaTime);
if (Input.GetKey (KeyCode.A))
transform.Translate (new Vector2 (-1, 0) * moveSpeed * Time.deltaTime);
}
//Used for checking if the player is touching the ground
void OnCollisionStay2D(Collision2D coll ) // C#, type first, name in second
{
if (coll.gameObject.tag == "Ground" &&(Input.GetKey(KeyCode.W)))
{
//Jump Script
rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode.Acceleration);
}
}
}
Answer by serenefox · May 19, 2014 at 09:08 PM
I remember reading somewhere on either Unity Answers or the Unity Forums that "ForceMode" does not work with rigidbody2D.Addforce, you have to take that out I am pretty sure.
Just did a quick search and this came up as the first hit, here is the link, hope this can get you going in to right direction: http://answers.unity3d.com/questions/574864/rigidbody2d-and-forcemodeaddvelocity.html
Ya, I figured it out and this seems to work.
rigidbody2D.AddForce(Vector3.up * jumpSpeed * Time.deltaTime);
Thanks again for your time.
I try a little and figured out (for Unity 5.5.2f1) that you can replace "Force$$anonymous$$ode.Acceleration" with "Force$$anonymous$$ode2D.Force. It also works.
rigidbody2D.AddForce(Vector2.up * jumpSpeed, Force$$anonymous$$ode2D.Force);
Answer by gaurav94 · Dec 16, 2017 at 06:30 PM
its solve in problem .............................................................................. transform.GetComponent().AddForce(Vector3.up*jumpSpeed,ForceMode.Acceleration);
Your answer
Follow this Question
Related Questions
Ground Detection Lagging - 2D 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers