提问人:HarrisonO 提问时间:7/20/2020 更新时间:7/20/2020 访问量:225
我怎样才能让我的“冲刺”推动我前进?
How can I make my "dash" push me forward?
问:
我是编程新手,想知道如何让我的“破折号”让我前进。我看了一个关于如何使破折号工作的在线教程,但在尝试之后,破折号只会在我写的情况下推动我。目前,当我按下破折号键时,我的角色的重力会减慢一秒钟,但没有移动。我能得到一些帮助来推动我的角色吗?rb.velocity = Vector2.up * dashSpeed;
如果您需要澄清,请询问!
PS:对不起,脚本太长了,我所有的玩家控制权都在这里。
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
//Player Movement
public float speed;
public float jumpForce;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float dashSpeed;
public float startDashTime;
public Animator animator;
private Rigidbody2D rb;
private float moveInput;
private bool isGrounded;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private float dashTime;
private int direction;
void Start()
{
animator.GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update()
{
// Moving
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
animator.SetBool("Moving", true);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
animator.SetBool("Moving", true);
}
else
{
animator.SetBool("Moving", false);
}
// Jumping
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("IsJumping");
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if (isGrounded == false)
{
animator.SetBool("Grounded", false);
}
if (isGrounded == true)
{
animator.SetBool("Grounded", true);
}
// Dashing
if(direction == 0)
{
if(Input.GetKeyDown(KeyCode.LeftShift))
{
if ((transform.rotation.eulerAngles.y == 180))
{
Dashleft();
}
else
{
DashRight();
}
}
}
else
{
if(dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
}
else
{
dashTime -= Time.deltaTime;
}
}
}
void Dashleft()
{
rb.velocity = Vector2.left * dashSpeed;
}
void DashRight()
{
rb.velocity = Vector2.right * dashSpeed;
}
}
答:
1赞
luvjungle
7/20/2020
#1
问题是你的水平速度在每个物理帧中被控制,所以当你在 或 方法中更改速度时,速度在这条线上被重置 你可以做这样的事情:FixedUpdate()
DashLeft()
DashRight()
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (dashTime <= 0)
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
因此,只有当玩家不冲刺时,您才能通过输入来控制速度
评论