c# - 使用 raycast2D 并让玩家旋转到 .hit 以防止他们跳跃
问题描述
和标题所说的差不多。将此脚本添加到我的玩家角色后:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateHit : MonoBehaviour
{
public string Tag;
public float offset;
private Transform target;
private Vector3 targetPos;
private Vector3 thisPos;
private float angle;
void Start()
{
target = GameObject.FindGameObjectWithTag(Tag).GetComponent<Transform>();
}
void LateUpdate()
{
targetPos = target.position;
thisPos = transform.position;
targetPos.x = targetPos.x - thisPos.x;
targetPos.y = targetPos.y - thisPos.y;
angle = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
}
}
每当我按下跳跃键时,音效都会播放,但它们不会移动。如果这很重要,我会尝试向rigidbody2D 增加力量以使它们跳跃。
我的跳转代码:
if (m_Grounded && jump)
{
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
bool jump = false;
if (Input.GetButtonDown("Jump"))
{
jump = true;
FindObjectOfType<AudioManager>().Play("Jump");
}
代码被拆分,因为每个部分都在不同的组件中。
解决方案
推荐阅读
- node.js - 关于使用 nodeJS MongoDb Mongoose 的 Swtich 案例的问题
- mysql - 在 mySQL 的表中生成(电话)号码序列
- amazon-web-services - 如何在 terraform 代码中使用 IAM 策略变量
- javascript - 关于javascript中setURL函数的问题
- python - 在 Laragom 中更新 Python - Windows 7 (ModuleNotFoundError: No module named 'pip')
- awk - 如何使用 for 循环从文本文件中提取数据并将其存储到新文件中?
- ios - Apple Watch 应用程序多久可以通过振动通知一次?
- r - 如何从调用环境而不是封闭环境中获取 R 函数的默认值
- c# - 在解决方案中也使用 C#/docker 项目调试在 Visual Studio 的 docker 容器中运行的 Linux C 项目?
- centos7 - 在 CentOS-7 中更新 /usr/include/c++