【发布时间】:2019-05-14 18:27:08
【问题描述】:
当拖动鼠标移动到鼠标的某个位置及其拖动功率时,我需要使用点或箭头类型符号以轨迹运动方式移动立方体。
我使用 2D 项目中使用的代码以轨迹运动方式移动球,但它不起作用。
这是我尝试的代码
void Start()
{
cam = Camera.main;
cubeClick = GameObject.Find("Cube Click Area");
trajectoryDots = GameObject.Find("Trajectory Dots");
rb = Player.GetComponent<Rigidbody>();
trajectoryDots.transform.localScale = new Vector3(
initialDotSize,
initialDotSize,
trajectoryDots.transform.localScale.z);
for (int dotNumber = 0; dotNumber < 40; dotNumber++)
{
Dots[dotNumber] = GameObject.Find("Dot (" + dotNumber + ")");
if (DotSprite !=null)
{
Dots[dotNumber].GetComponent<SpriteRenderer>().sprite = DotSprite;
}
}
for (int dotNumber = NumberOfDots; dotNumber < 40; dotNumber++)
{
GameObject.Find("Dot (" + dotNumber + ")").SetActive(false);
}
trajectoryDots.SetActive(false);
}
void Update()
{
Ray camRay = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//RaycastHit2D hit = Physics2D.Raycast(cam.(Input.mousePosition), Vector2.zero);
if (Physics.Raycast(camRay, out hit, 100f) && !cubeisClicked2)
{
if (hit.collider.gameObject.name == Player.name)
{
cubeisClicked = true;
}
else
{
cubeisClicked = false;
}
}
else
{
cubeisClicked = false;
}
if (cubeisClicked2)
{
cubeisClicked = true;
}
if ((rb.velocity.x * rb.velocity.x) + (rb.velocity.y * rb.velocity.y) <= 0.0085f)
{
rb.velocity = new Vector2(0f, 0f);
}
else
{
trajectoryDots.SetActive(false);
}
cubePosition = Player.transform.position;
if ((Input.GetKey(KeyCode.Mouse0) && cubeisClicked) && ((rb.velocity.x == 0f && rb.velocity.y == 0f)))
{
cubeisClicked2 = true;
fingerPosition = cam.ScreenToWorldPoint(Input.mousePosition);
fingerPosition.z = 0f;
cubeFingerDiff = cubePosition - fingerPosition;
ShotForce = new Vector2(cubeFingerDiff.x * shootingPowerX , cubeFingerDiff.y * shootingPowerY);
if (Mathf.Sqrt((cubeFingerDiff.x * cubeFingerDiff.x) + (cubeFingerDiff.y * cubeFingerDiff.y) )>0.4f)
{
trajectoryDots.SetActive(true);
}
else
{
trajectoryDots.SetActive(false);
}
for (int dotNumber = 0; dotNumber < NumberOfDots; dotNumber++)
{
x1 = cubePosition.x * ShotForce.x * Time.fixedDeltaTime * (DotSeparation * dotNumber * dotShift);
y1 = cubePosition.y * ShotForce.y * Time.fixedDeltaTime * (DotSeparation * dotNumber * dotShift) - (-Physics.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (DotSeparation * dotNumber + dotShift) * (DotSeparation * dotNumber + dotShift));
Dots[dotNumber].transform.position = new Vector3(x1, -y1, 0);
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
cubeisClicked2 = false;
trajectoryDots.SetActive(false);
rb.velocity = new Vector2 (ShotForce.x, ShotForce.y);;
}
}
}
它不起作用,它显示了如图所示的点。
它应该以用户拖动并指向要移动的位置的方式移动立方体。
【问题讨论】:
-
它做什么而不是工作?结果与您期望的代码有何不同?
-
请描述如何它不起作用
-
@Ruzihm 它不起作用意味着它只是显示如上图所示的点,无法通过按住帮助鼠标左键并拖动来移动点,也没有拍摄释放按钮时的立方体
-
您将
cubePosition.x *更改为cubePosition.x +和cubePosition.y *更改为cubePosition.y +并且单击和拖动时仍然得到相同的结果?另外,您是否将ShootingPowerX或ShootingPowerY设置为0? -
@Ruzihm 我将 cubePosition.x * 更改为 cubePosition.x +,将 ShootingPowerX 更改为 6,将 ShootingPowerY 更改为 -1.5 它可以正确绘制,但不会随着鼠标移动而移动,并且不会在每个节点上跳跃鼠标离开,但有时它会离开
标签: c# unity3d game-physics