Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Unity Top-Down Dashing

$
0
0

Trying to Create Dash feature to my top-down game and when I press space I can see the velocity changing by looking at the console but in actual gameplay it won't change a bit.What am I missing? I know there's a lot of lines that are not mature enough but please focus on the feature of a dash function.

using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerController : MonoBehaviour{    private const float DOUBLE_TAP_TIMELIMIT = 0.2f;    public float walkSpeed = 5f;    public float sprintSpeed = 10f;    private bool canDash = true;    private bool isDashing;    public float dashingPower = 24f;    private float dashingTime = 0.2f;    private float dashingCoolDown = 1f;    private Rigidbody2D rigid;    public TrailRenderer tr;    private float horz;    private float vert;    private bool HorV;    private bool isSprinting=false;    void Awake()    {        rigid = GetComponent<Rigidbody2D>();        tr = GetComponent<TrailRenderer>();    }    void Update()    {        if(isDashing){            return ;        }        if (Input.GetKeyDown(KeyCode.LeftShift))            isSprinting = true;        else if (Input.GetKeyUp(KeyCode.LeftShift))            isSprinting = false;        horz = Input.GetAxisRaw("Horizontal");        vert = Input.GetAxisRaw("Vertical");        bool hDown = Input.GetButtonDown("Horizontal");        bool hUp = Input.GetButtonUp("Horizontal");        bool vDown = Input.GetButtonDown("Vertical");        bool vUp = Input.GetButtonUp("Vertical");        //Disallowing Diagonal Movement        if (hDown || vUp)            HorV = true;        else if (vDown || hUp)            HorV = false;        if (Input.GetKeyDown(KeyCode.Space) && canDash)            StartCoroutine(Dash());    }    void FixedUpdate()    {        if (!isSprinting)        {   //Walking            if (HorV)                rigid.velocity = new Vector2(horz, 0) * walkSpeed;            else                rigid.velocity = new Vector2(0, vert) * walkSpeed;        }        else        {   //Sprinting            if (HorV)                rigid.velocity = new Vector2(horz, 0) * sprintSpeed;            else                rigid.velocity = new Vector2(0, vert) * sprintSpeed;        }    }    private IEnumerator Dash() {        canDash = false;        isDashing = true;        if(HorV)            rigid.velocity = new Vector2(horz,0)*dashingPower;        else             rigid.velocity = new Vector2(0,vert)*dashingPower;        Debug.Log(rigid.velocity);        tr.emitting = true;        yield return new WaitForSeconds(dashingTime);        tr.emitting = false;        isDashing = false;        yield return new WaitForSeconds(dashingCoolDown);        canDash = true;    }}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>