- Home /
Even / Constant Speed and Jumping 2D
I making a 2D game. I want it to have a constant speed. Equal at all times. I'm not sure how to do this. I've tried AddForce and Velocity but it always runs out. I add a speed and then it slows down over time. I've tried adding it once in a while but it either loses too much speed in the end or gains to much speed. I'm making one of the running games that need a even speed like Subway Surfers, Slither.io, and Dune. I also need jumping but it can't be constant because it shouldn't just fly into the sky lol. But, I also need that because with force it depends on how early they jump and it is hard because it can't just fly into the sky but it shouldn't depend on how early. My Code:
using UnityEngine;
public class PlayerForce : MonoBehaviour {
public int movementForce = 10;
public Rigidbody2D rb2D;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void OnTriggerEnter2D(Collider2D other)
{
rb2D.velocity = new Vector3(movementForce * Time.deltaTime, 0, 0);
}
}
Jumping
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && Jumping == false)
{
rb2D.AddForce(transform.up * jump * Time.deltaTime);
Invoke("PushDown", pushdowndelay);
Jumping = true;
Invoke("TurnJumpingOn", 1f);
}
}
void TurnJumpingOn()
{
Jumping = false;
}
void PushDown()
{
rb2D.AddForce(transform.up * pushDown * Time.deltaTime);
}
watch this,it might be helpful.
////////Character $$anonymous$$ovement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : $$anonymous$$onoBehaviour {
public float moveSpeed=5;
public float jumpHeight=15;
public Rigidbody2D rig;//with this component name RigidBody2D we Can move objects
public Animator anim;//so we can Access to the animator window
public int Grounded;// 0 means Grounded *** 1 means not grounded
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody2D> ();
anim=GetComponent<Animator>();
anim.Play ("run");
}
void Update(){
rig.velocity = new Vector2 (moveSpeed,rig.velocity.y);
if (Input.GetButtonDown ("Jump")&& Grounded==1) {
rig.velocity = new Vector2 (rig.velocity.x,jumpHeight);
}
}
Ground's script//when you're on the Ground , you can jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ground : $$anonymous$$onoBehaviour {
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.CompareTag ("player")) {
other.gameObject.GetComponent<movement> ().Grounded = 1;//it means that she's moving on the ground
}
}
//:)
void OnCollisionExit2D(Collision2D other){
if (other.gameObject.CompareTag ("player")) {
other.gameObject.GetComponent<movement> ().Grounded = 0;//it means that she's not moving on the ground anymore
}
}
Hey @Persian$$anonymous$$iller, I tried this and 1, the jumping wasn't working and 2, I made a running animation but idk how to turn it off to test if the script plays it. If anyone could help it would be great!
Answer by Honorsoft · Jan 10, 2018 at 04:43 PM
I'm still learning the in's and out's in Unity too, but I think that Unity applies a default 'angular Drag' setting (in the inspector, near the 'use Gravity' toggle), maybe that is applying a gradual stop to your object, try shutting it off. If you want controlled speed and jumping, maybe pick a simpler way of doing it in Unity, rigidbody physics are afftected by gravity, 'wind', other rigidbody shapes, etc. You can use Unity's animator to make a jump or movement instead. I used AddForce to make a jump and my object would occasionally bounce, flip and fall over. I used the animator instead and made a smooth, stable and realistic looking jump.
Anyone let me know what you think about the best ways to move and jump...
Okay, good idea, but do you know what a script for a animation like that would look like? I've not very good with animation scripts. Also how would you make it smoothly go from move animation - jump animation - move animation? @Honorsoft