C# 我怎样才能使我的;破折号;推动我前进?
我是编程新手,想知道如何让我的“冲刺”让我前进。我看了一个关于如何使短跑有效的在线教程,但是在尝试之后,如果我写了C# 我怎样才能使我的;破折号;推动我前进?,c#,unity3d,C#,Unity3d,我是编程新手,想知道如何让我的“冲刺”让我前进。我看了一个关于如何使短跑有效的在线教程,但是在尝试之后,如果我写了rb.velocity=Vector2.up*dashSpeed,短跑只会推动我。目前,当我按下破折号键时,角色的重力会减慢一秒钟,但不会移动。我能得到一些帮助,让它推动我的角色吗 如果您需要澄清,请询问 对不起,剧本太长了,我所有的玩家控制权都在这里 using System.Collections; using System.Collections.Generic; using
rb.velocity=Vector2.up*dashSpeed,短跑只会推动我代码>。目前,当我按下破折号键时,角色的重力会减慢一秒钟,但不会移动。我能得到一些帮助,让它推动我的角色吗
如果您需要澄清,请询问
对不起,剧本太长了,我所有的玩家控制权都在这里
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;
}
}
使用系统集合;
使用System.Collections.Generic;
使用System.Security.Cryptography;
使用UnityEngine;
公共类CharacterController:单行为
{
//球员运动
公众浮标速度;
公共安全部队;
公共服务;
公共浮动校验半径;
公共层码头;
公共交通速度;
公众浮标启动时间;
公共动画师;
私有刚体2d rb;
私有浮点输入;
私人学校停课;
专用浮点跳时计数器;
公共时间;
私家侦探在跳;
私人时间;
私人int方向;
void Start()
{
GetComponent();
rb=GetComponent();
dashTime=开始dashTime;
}
void FixedUpdate()
{
moveInput=Input.GetAxisRaw(“水平”);
rb.velocity=新矢量2(移动输入*速度,rb.velocity.y);
}
无效更新()
{
//移动的
IsGround=Physics2D.重叠圆(触角位置、检查半径、whatIsGround);
如果(移动输入>0)
{
transform.eulerAngles=新矢量3(0,0,0);
动画师.SetBool(“移动”,真);
}
else if(移动输入<0)
{
transform.eulerAngles=新矢量3(0,180,0);
动画师.SetBool(“移动”,真);
}
其他的
{
animator.SetBool(“移动”,false);
}
//跳跃
if(isground==true&&Input.GetKeyDown(KeyCode.Space))
{
设置触发器(“IsJumping”);
isJumping=true;
jumpTimeCounter=跳跃时间;
rb.速度=矢量2.向上*跳跃力;
}
if(Input.GetKey(KeyCode.Space)和&isJumping==true)
{
如果(跳时计数器>0)
{
rb.速度=矢量2.向上*跳跃力;
jumpTimeCounter-=Time.deltaTime;
}
其他的
{
isJumping=false;
}
}
if(Input.GetKeyUp(KeyCode.Space))
{
isJumping=false;
}
如果(isground==false)
{
animator.SetBool(“接地”,错误);
}
如果(isGrounded==真)
{
动画师。SetBool(“接地”,真);
}
//冲刺的
如果(方向==0)
{
if(Input.GetKeyDown(KeyCode.LeftShift))
{
if((transform.rotation.eulerAngles.y==180))
{
Dashleft();
}
其他的
{
DashRight();
}
}
}
其他的
{
如果(dashTime问题是您的水平速度在FixedUpdate()中被控制
它在每个物理帧中都被覆盖,因此当您在DashLeft()
或DashRight()
方法中更改速度时,这一行的速度被重置rb.velocity=new Vector2(moveInput*speed,rb.velocity.y);
您可以这样做:
if (dashTime <= 0)
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(dashTime)