简体   繁体   中英

How can I add velocity to a player in Unity?

I'm currently making a 2D game and I want to add a bounce pad. As a reference, I used the platformer microgame asset from the asset store. My problem is, that my player doesn't have velocity. Can someone explain me pls how I can do this? Or could you add the code to my current script because I'm not very keen on coding.

Thanks!

Current script:

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

public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;
    public Animator animator;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

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

        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
            animator.SetBool("IsJumping", true);
        }

        if (Input.GetButtonDown("Crouch"))
        {
            crouch = true;
        } else if (Input.GetButtonUp("Crouch"))
        {
            crouch = false;
        }

    }

    public void OnLanding ()
    {
        animator.SetBool("IsJumping", false);
    }

    public void OnCrouching (bool isCrouching)
    {
        animator.SetBool("IsCrouching", isCrouching);
    }

    void FixedUpdate ()
    {
        // Move our character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
    }
}

You should use Raycast to go directly down and see if the player is above a “bounce pad”. Then calculate the distance, normal, and force in the code below:

float bounceDist = 0.2f; //max distance to hit the bounce pad.
LayerMask bounceMask; //assign mask to bounce pads.
float force = 100f; //how much force to apply when hit a bounce pad.

void Update()
{
    RaycastHit2D hit; //declaring a variable to store Raycast information.
    if (Physics2D.Raycast(transform.position, -Vector2.up, out hit, bounceDist, bounceMask))
    {
        float dist = Vector2.Distance(transform.position, hit.collider.gameObject.transform.position);
        dist = -dist + (bounceDist + bounceDist - 0.15f);
        controller.Move(hit.normal * Time.deltaTime * dist * force);
    }
}

This makes an if statement that runs if the player hit something that is directly below it with a certain distance (bounceDist) and another certain mask (bounceMask). Then it moves the controller by whatever direction is up on bounce pad (normal). (For example: if it was rotated 45°, then the player would be launched the same up distance as sideways.) Then it gets multiplied by Time.deltaTime , which makes it so the force is applied the same amount even with different frame rates. Then the force is multiplied by the value of force , just to add force. Then it is multiplied by the distance, which I made greater when closer. This can be removed, but might be useful. If you get an error with the if statement, you should change the code to use the if statement to check if the tag on the hit object was a certain tag: You should use Raycast to go directly down and see if the player is above a “bounce pad”. Then calculate the distance, normal, and force in the code below:

float bounceDist = 0.2f; //max distance to hit the bounce pad.
float force = 100f; //how much force to apply when hit a bounce pad.

void Update()
{
    RaycastHit2D hit; //declaring a variable to store Raycast information.
    Physics2D.Raycast(transform.position, -Vector2.up, out hit, bounceDist, bounceMask);
    if (hit.collider.gameObject.tag == “bouncePad”)
    {
        float dist = Vector2.Distance(transform.position, hit.collider.gameObject.transform.position);
        dist = -dist + (bounceDist + bounceDist - 0.15f);
        controller.Move(hit.normal * Time.deltaTime * dist * force);
    }
}

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