我目前正在制作一个2D游戏,我想添加一个弹跳板。作为参考,我使用了来自资产商店的平台微型游戏资产。我的问题是,我的球员没有速度。有人能解释一下我是怎么做到的吗?或者,您可以将这些代码添加到我当前的脚本中,因为我不太喜欢编码。
谢谢!
当前脚本:
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;
}
}发布于 2021-05-03 16:52:20
你应该使用雷卡斯特直接下去,看看球员是否在一个“弹跳板”之上。然后在下面的代码中计算出距离、法线和力:
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);
}
}这就产生了一个if语句,如果玩家用一定的距离(bounceDist)和另一个特定的掩码(bounceMask)直接击中它下面的某个东西,它就会运行。然后,它移动控制器的任何方向上的反弹垫(正常)。(例如:如果它被旋转了45°,那么玩家将被发射到与侧面相同的向上距离。)然后将其乘以Time.deltaTime,使其在不同的帧速率下施加相同的力。然后把力乘以force的值,只为了增加力。然后,它被乘以距离,这是我使之更近的时候。这可以删除,但可能是有用的。如果If语句出现错误,则应该更改代码以使用if语句来检查击中对象上的标记是否为特定标记:您应该使用Raycast直接向下看播放机是否在“弹跳板”之上。然后在下面的代码中计算出距离、法线和力:
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);
}
}https://stackoverflow.com/questions/67371329
复制相似问题