c# - 我如何使光线投射旋转并使脚本在接触时激活
问题描述
所以我试图制作投掷和抓取脚本。
光线投射仅在右侧,我不知道如何使其旋转并在与特定对象碰撞时激活。好吧,我不确定如何改进它或让它按我想要的方式工作。我试图让它朝着你正在摆动的方向投掷,但我几乎不知道如何实现这一目标。
using UnityEngine;
using System.Collections;
public class grabberscript : MonoBehaviour
{
public bool grabbed;
RaycastHit2D hit;
public float distance = 2f;
public Transform holdpoint;
public float throwforce;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.B))
{
if (!grabbed)
{
Physics2D.queriesStartInColliders = false;
hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance);
if (hit.collider != null && hit.collider.tag == "grabbable")
{
grabbed = true;
}
//grab
}
else if (!Physics2D.OverlapPoint(holdpoint.position, notgrabbed))
{
grabbed = false;
if (hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
{
hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwforce;
}
//throw
}
}
if (grabbed)
hit.collider.gameObject.transform.position = holdpoint.position;
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position + Vector3.right * transform.localScale.x * distance);
}
}
解决方案
推荐阅读
- python - 在我的算法运行良好后,我的数组返回全零
- android - 在 Godot 中制作的 android 应用程序的签名密钥
- .net-core - .NET Core 3.1 上的 Azure Functions 3 抛出 OutOfMemoryException
- python - docker中的python3 mayavi没有安装
- mysql - 协助在不使用实际数据的情况下向表中添加记录
- java - 有没有办法在另一个方法中使用一个方法的返回值?
- mongodb - Mongo 搜索数组,在 $or 中只有一个匹配项
- javascript - attributeList.push() 在 cognito 中不起作用
- reverse-engineering - 运行 AFL-Fuzzer 缓冲区溢出
- python - 创建一个循环以遍历具有相同变量名称的不同数据集以更改值