- Home /
Duplicate Question
About RigidBody2D.AddForce()...(Revised without spelling Errors)
Can I please have a actual answer to my question:
Hey, I need some help with my coding. I'm trying to figure out how to create jumping easily and without much to code, unfortunately I found that I do not seem to have Rigidbody2D.AddForce() for my drop-down list to insert. I want to learn how to make characters able to jump in the game, but I am unsure of other methods that can be made on the fly--as I've seen a load of people in need of help use AddForce for making their characters jump. Is there anyway that I can still incorporate this method in my Unity 5.1.f03 version, or do I need to do something a lot more complicated and different?
EDIT: I apologize for my rudeness, but I actually need some help with this as I really, really want to make a video-game by myself.
Ins$$anonymous$$d of editing to apologise, why not edit out the rudeness?
Write a proper question, get a proper answer.
You didn't even search google. The first hit makes this a duplicate question.
Answer by YoungDeveloper · Jul 29, 2015 at 01:05 AM
Mod who told you about spelling mistakes is 100% right. If OP can't invest enough energy and work for nicely formatted question, why possible answer givers must break their heads thinking what u meant with what. It's consider totally disrespectful to throw garbage text at someone and expect them to invest their time helping you. #endmoral
What comes to your question, yes, you can use add force for rigidbody to simulate jump effect, or write hard coded custom jump which doesn't take physics. Add force isn't any different from previous versions (from syntax perceptive), so its the same. You clearly haven't included any code or actual tried anything, so there's that. Internet and Youtube is full or any kind of character controller tutorials (3d and 2d), so it would be arrogant to say that there is no information on this topic.
Answer by MegaChuck64 · Jul 29, 2015 at 01:11 AM
well you should definitely have that as an option. Make sure use GetComponent first. like this
using UnityEngine;
using System.Collections;
public class JumpScript : MonoBehaviour {
Rigidbody2D rB;
public float jumpSpeed = 5;
void Start ()
{
rB = GetComponent<RigidBody2D>();
}
void Update()
{
if (Input.GetKeyDown(Keycode.Space))
{
rB.AddForce(transform.up * jumpSpeed);
}
}
Alright, I've added the GetComponent in the way you've done it, but now it seems that only one jump is allowed before it returns to it not being able to use the Space input the way it should. I'll go on ahead and look up how to fix that. Thank you very much!
try adding "Time.deltaTime" after rB.AddForce(transform.up * jumpSpeed.
rB.AddForce(transform.up * jumpSpeed * Time.deltaTime);
if you put something in the "void Update", it happens once per frame. so unless you have a really high frame rate, your code will only work sometimes. Since it has to start right on a frame. so if you click jump in between frames it wont work. "Time.deltaTime" multiplies by the time in seconds it took to complete the last frame, creating a seamless jump that works every time.
Follow this Question
Related Questions
Player controller jump not always working 2 Answers
Jumping behavior based on what part of an object you're touching? 0 Answers
Please Help me fix my code (jumping animation playing once) 0 Answers
Jump Script Not working (3D) 2 Answers
Can't implement jumping to 2D side-scroller click-to-move. 1 Answer