-2
黑家伙,我必须做出使刚体球移动刚体球当我点击鼠标右键点击时。 **我得到了一个大问题,程序StackOverFlow,我不知道如何使它运行良好。也许问题是因为我打电话递归我的方法,但我真的没有任何其他ideea **Eroor计算器试图移动使用鼠标右键点击
还有就是我在我的剧本确实到现在为止:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Bundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
[Range(1, 20)]
private float speed;
private Vector3 targetPosition;
private bool isMoving;
public Rigidbody rigidBody;
public Bundary bundary;
const int RIGHT_MOUSE_BUTTON = 1;
private void Start()
{
targetPosition = transform.position;
isMoving = false;
}
/// <summary>
/// Sets the travel position where we will travel
/// </summary>
void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point))
targetPosition = ray.GetPoint(point);
//set the ball to move
isMoving = true;
}
void FixedUpdate()
{
transform.LookAt(targetPosition);
Vector3 movement = new Vector3(Input.mousePosition.x, 0.0f, Input.mousePosition.z); //how much we want to move
rigidBody.velocity = movement * speed; //because movement return a value bettwen (0,1) we want the ball move faster so multiply it with our speed
rigidBody.position = new Vector3
(Mathf.Clamp(rigidBody.position.x, bundary.xMin, bundary.xMax), //set x limits with Clamp method
0.0f, //we don't want to move it on Y axex
Mathf.Clamp(rigidBody.position.z, bundary.zMin, bundary.zMax)); //set y limits with Clamp method
//if we are in the target position ,then stop moving !
if (movement == targetPosition)
isMoving = false;
//if the player clicked on the screen , found out where
if (Input.GetMouseButton(RIGHT_MOUSE_BUTTON))
SetTargetPosition();
//if we are still moving ,then move the player
if (isMoving)
FixedUpdate();
}
}
还有就是我的计划应该在最后阶段做的:http://prntscr.com/fqu98u
你忘了提到这个问题。 – Programmer
我得到了一个大问题,程序StackOverFlow,我不知道如何使它运行fine.i写它,仔细阅读 –
如果你得到一个错误应该发布错误。此外,请通过双击该错误告诉我们导致该错误的代码行。这是一个自上而下还是侧视游戏? – Programmer