简体   繁体   中英

Problem with Jumping with Translate function [Unity 2D]

Soo, I'm trying to code a simple Platformer as a work for class, and I'm having problems with the jumping, the character actually jumps but it seems as a teleport since it instantly reaches the peak of the jump and then gradually falls, I'd like to make it smoother.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerMovement : MonoBehaviour {

    public float speed;
    public float jumpForce;
    public bool grounded;
    public Transform groundCheck;
    private Rigidbody2D rb2d;

    // Use this for initialization
    void Start () {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update () {
    }

    private void FixedUpdate()
    {
        float moveVertical = 0;
        float moveHorizontal = Input.GetAxis("Horizontal") * speed;
        moveHorizontal *= Time.deltaTime;

        grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

        if (grounded && Input.GetKeyDown("space"))
        {
            moveVertical = Input.GetAxis("Jump") * jumpForce;
            moveVertical *= Time.deltaTime;
        }

        Vector2 move = new Vector2(moveHorizontal, moveVertical);
        transform.Translate(move);
    }
}

The answer to the question is below, however, for future questions related to Unity ask on the Unity forum. You will get a better answer quicker as more people are focused on Unity itself.

To fix your jump, rather split your vertical and horizontal movement in two parts and use your rigidbody you assigned in in your Start() function to handle jumping. Use ForceMode.Impulse to get an instant burst of speed/acceleration.

private void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal") * speed;
    moveHorizontal *= Time.deltaTime;

    grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

    if (grounded && Input.GetKeyDown("space"))
    {
        rb2d.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    }

    Vector2 move = new Vector2(moveHorizontal, 0);
    transform.Translate(move);
}

Instead of setting the position when you jump, add a vertical impulse to the character's rigidbody with:

if (grounded && Input.GetKeyDown("space"))
{
    moveVertical = Input.GetAxis("Jump") * jumpForce;
    moveVertical *= Time.fixedDeltaTime;
    rb2d.AddForce(moveVertical, ForceMode.Impulse);
}

Vector2 move = new Vector2(moveHorizontal, 0f);

This will let Rigidbody2D handle the work of altering the vertical velocity on each frame.

Also, since you are doing all of these calculations in FixedUpdate , instead of Update , there is no sense in using Time.deltaTime . You should be using Time.fixedDeltaTime instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM