2016-09-28 23 views
0

我试图让我的球员和最近的对象之间的距离与标记'墙',但我似乎无法得到它的工作。 据我所知,我的代码根本不工作。 所以我的问题是; 我在做什么错?再次,我想找到我的球员和最近的对象与标签'墙'的距离。如果我靠近带有'wall'标签的对象,我希望它将变量设置为true(nearWall = true),那么一旦我离开对象(大约10.0f),我希望它返回false。 nearWall = false)获取墙壁的距离(具有相同标记的多个对象)

这是我一直在使用的代码。

using UnityEngine; 
using System.Collections; 

public class PlayerMotor : MonoBehaviour { 

    private CharacterController controller; 
    private Vector3 moveVector; 
    private float speed = 2.0f; 
    private float verticalVelocity = 0.0f; 
    private float gravity = 12.0f; 

    private bool nearWall; 
    public GameObject playerObject; 

    GameObject closestObject; 

    float distance = Mathf.Infinity; 

    public float distanceToWall = Mathf.Infinity; 


    private void Start() { 
     nearWall = false; 
     playerObject = GameObject.Find("Player"); 

     distanceToWall = 0; 
     controller = GetComponent<CharacterController>(); 
    } 
    public void getNearestWall() 
    { 
     if (distance <= 10.0f) { 
      nearWall = true; 
      print ("Near wall!"); 
     } 
     else 
      nearWall = false; 
    } 
    GameObject findNearestWall() 
    { 
     GameObject[]objectArray; 
     objectArray = GameObject.FindGameObjectsWithTag("wall"); 

     Vector3 position = playerObject.transform.position; 

     foreach(GameObject currentObject in objectArray) 
     { 
      Vector3 distanceCheck = currentObject.transform.position - position;  
      float currentDistance = distanceCheck.sqrMagnitude; 

      if (currentDistance < distance) 
      { 
       closestObject = currentObject; 

       distance = currentDistance; 

      } 
     } 

     return closestObject; 
    } 
    private void Update() 
    {   
     findNearestWall(); 
     moveVector = Vector3.zero; 
     if (controller.isGrounded) 
     { 
      verticalVelocity = -0.5f; 
     } 
     else 
     { 
      verticalVelocity -= gravity * Time.deltaTime; 
     } 
     if (Input.GetMouseButton (0)) { 
      if (!nearWall) { 
       if (Input.mousePosition.x > Screen.width/2) 
        moveVector.x = speed; 
       else 
        moveVector.x = -speed; 
      } 
      else 
      { 
       moveVector.x = transform.forward.x * speed; 
       transform.Rotate(new Vector3(0, -90, 0)); 
      } 
     } 
     moveVector.y = verticalVelocity; 
     moveVector.z = transform.forward.z * speed; 
     controller.Move (moveVector * Time.deltaTime); 
    } 
} 
+0

也许尝试使用光线投射测量距离,你将不必存储在阵列中的所有游戏对象,可以从球员的位置或从屏幕出来做,如果它是第一人称游戏。 –

回答

0

一件事是,你是不是叫getNearestWall()方法 - 这实际上是改变标志 - 任何地方。 其次你为什么不只是尝试:

currentDistance = Vector3.Distance(currentObject.transform.position, position); 

在计算距离

0

首先你需要调用getNearestWall();Update()方法里面(当然findNearestWall()之后)。你现在正在做的是让玩家在整个游戏中达到的最小距离。你可能想在findNearestWall()顶部添加distance = Mathf.Infinity;所以它会是这样的:

GameObject findNearestWall() 
{ 
    GameObject[] objectArray; 
    objectArray = GameObject.FindGameObjectsWithTag("wall"); 
    distance = Mathf.Infinity; 
    Vector3 position = playerObject.transform.position; 

    foreach (GameObject currentObject in objectArray) 
    { 
     Vector3 distanceCheck = currentObject.transform.position - position; 
     float currentDistance = distanceCheck.sqrMagnitude; 

     if (currentDistance < distance) 
     { 
       closestObject = currentObject; 
       distance = currentDistance;    
     } 
    } 

    return closestObject; 
} 

现在,只要你靠近墙壁它应该打印Near wall!

注: 你也正以更新呼叫FindObjectsWithTag()方法可能会大大消耗您的处理能力。您可能希望通过在课堂中声明私人GameObject[] objectArray来避免这种情况。 然后在使用objectArray = GameObject.FindGameObjectsWithTag("wall");一次Awake()Start()

相关问题