c# - 使用 Unity 启动与 Line Renderer 相对的播放器时出现问题
问题描述
所以我希望我的播放器在我制作的线渲染器的对面启动。线渲染器工作正常,是我想要的样子。但是我无法尝试使物理起作用。我一直在浏览 Unity 文档和在线帖子,但仍然没有运气。
任何帮助将不胜感激。
线路启动脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineLaunching : MonoBehaviour
{
public float power;
public float dragLength;
public Rigidbody2D rb;
public LineRenderer lineRend;
public Camera cam;
private Vector3 startPoint;
private Vector3 endPoint;
private Vector2 force;
void Awake()
{
power = 100f;
}
void Update()
{
if (Input.GetMouseButton(0))
{
startPoint = gameObject.transform.position;
startPoint.z = 15;
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
RenderLine(startPoint, currentPoint);
rb.velocity = new Vector2(0, 0);
}
if (Input.GetMouseButtonUp(0))
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
if (startPoint.x > endPoint.x && startPoint.y > endPoint.y)
{
rb.velocity = new Vector2(startPoint.x - endPoint.x * power * Time.deltaTime, startPoint.y - endPoint.y * power * Time.deltaTime);
}
if (startPoint.x > endPoint.x && startPoint.y < endPoint.y)
{
rb.velocity = new Vector2(startPoint.x - endPoint.x * power * Time.deltaTime, endPoint.y - startPoint.y * power * Time.deltaTime);
}
if (startPoint.x < endPoint.x && startPoint.y > endPoint.y)
{
rb.velocity = new Vector2(endPoint.x - startPoint.x * power * Time.deltaTime, startPoint.y - endPoint.y * power * Time.deltaTime);
}
if (startPoint.x < endPoint.x && startPoint.y < endPoint.y)
{
rb.velocity = new Vector2(endPoint.x - startPoint.x * power * Time.deltaTime, endPoint.y - startPoint.y * power * Time.deltaTime);
}
lineRend.positionCount = 0;
}
}
public void RenderLine(Vector3 startPoint, Vector3 endPoint)
{
lineRend.positionCount = 2;
Vector3[] points = new Vector3[2];
points[0] = startPoint;
points[1] = Vector3.MoveTowards(startPoint, endPoint, 6.5f);
lineRend.SetPositions(points);
float fade = Vector2.Distance(startPoint, endPoint) / 100 * 15;
lineRend.startColor = new Color(1.0F, fade + 1 - (fade * 2), 0.0F, 1.0F);
lineRend.endColor = new Color(1.0F, fade + 1 - (fade * 2), 0.0F, 0.0F);
}
}
这里还有一个 gif,它显示了它在 Unity 中运行时的样子。
运行时 Unity 的 Gif:
解决方案
我只会从你的start
to中获取方向向量end
并否定它。它应该为您提供与您瞄准的完全相反的方向。如果您对速度进行检查,它也会减少复杂性。我还建议使用AddForce
而不是直接设置速度,因此如果您希望其他外力作用在对象上,则它们不会被此代码覆盖。
if (Input.GetMouseButtonUp(0))
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
Vector2 negatedDirection = (startPoint - endPoint).normalized;
rb.AddForce(negatedDirection * power, ForceMode2D.Impulse);
lineRend.positionCount = 0;
}
您还可以使用另一种ForceMode2D
方法,但是您似乎希望运动立即进行,Impulse
这是要走的路。
推荐阅读
- android - 如何修复 EBUSY:Ionic 3 中的资源繁忙或锁定?
- javascript - 如何在 javascript 中使用 fetch api 从 C# Web 方法获取响应?
- javascript - js - querySelector 在伪类 ::before 中选择和编辑样式
- html - 在 org.apache.pdfbox.pdmodel.graphics.color.PDPattern.toRGB 获取 java.lang.UnsupportedOperationException
- python - 列表字典的键到行枚举、制表符分隔的 csv 文件
- jmeter - 如何计算 JMeter Webdriver 采样器中两个不同操作的时间
- ruby-on-rails - 为什么 gem 在用户登录后设计重定向到注册路径?
- javascript - 在 AngularJS 表格组件中计算值
- javascript - 浏览器zendframework找不到javascript脚本源
- javascript - 在javascript中更改按钮的背景颜色不起作用