c# - 统一触摸某个图层时如何重置播放器的位置?
问题描述
当我触摸“死亡”层时,我正在尝试重置我的玩家的位置(到 0、2、0)。我该怎么做?
我试过使用bool
被调用isDead
,我的代码看起来像这样:
if(isDead)
{
transform.position = new Vector3(0f, 2f,0f);
}
这是我的全部代码,可能会有所帮助
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
scripts
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float jumpHeight = 3f;
Vector3 velocity;
bool isGrounded;
bool isDead;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
for evig når den treffer bakken
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
}
}
我不知道该怎么做。有什么建议么?谢谢!
解决方案
如果死亡层上的碰撞器是触发器,则需要OnTriggerEnter,否则需要OnCollisionEnter。例子:
private void OnCollisionEnter(Collision collision)
{
transform.position = new Vector3(0f, 2f, 0f);
}
推荐阅读
- c - sqlite3_bind_text() 不绑定占位符
- javascript - 在 Angular 自定义库中读取 Json 文件
- c++ - 使用库时如何修复“错误 C1083:无法打开包含文件:'simpleclass.h':没有这样的文件或目录”?
- c - 在 Linux 中通过 prctl() 更改可执行文件名
- android - ConstraintLayout 的 setDesignInformation 方法的用途
- python - 合适的元启发式优化方法来解决问题
- python - format() 函数在 jupyter 中生成科学数字
- sql - 如何从 BigQuery JavaScript UDF 为字符串化几何集合中的每个要素创建几何?
- python - 训练 CNN 的结果看似不错,但测试时效果不佳
- spring-data-r2dbc - R2DBC 映射以在数据库中的表之间建立关系