1

Unity には、ClosestTarget 関数を除いてすべて正常に動作するプレーヤー クラスがあります。関数は希望どおりに機能しますが、別のキューブに近づいても、キューブ 2 (リストの最後の要素) しか選択されません。

クラスは次のとおりです。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour
{

    public int health; //Current health
    public int stamina; //Current stamina
    public int maxHealth = 100; //Constant for max health
    public int maxStamina = 500; //Constant for max stamina
    protected CharacterController chCtrl; //Reference to the character controller
    protected CharacterMotor chMotor; //Reference to the character motor
    public float walkSpeed = 3; //Speed at which the player walks
    public float runSpeed = 20; //Speed at which the player runs
    public bool isWalking = false; //Check for whether the player is walking
    public bool isRunning = false; //Check for whether the player is running
    public bool isFatigued = false; //Check for whether the player is fatigued
    public List<Transform> targets; //Create a list of transforms
    public Transform currentTarget;
    float distance = Mathf.Infinity;

    // Use this for initialization
    void Start ()
    {
        //Get the character controller assigned to the current game object
        chCtrl = GetComponent<CharacterController> ();
        //Get the character motor assigned to the current game object
        chMotor = GetComponent<CharacterMotor> ();
        //Set the stamina to the max stamina
        stamina = maxStamina;
        //Set the health to the max health
        health = maxHealth;
        //Initialise the targets list
        targets = new List<Transform>();
        //Call the function to retrieve all buttons in the scene
        AddAllButtons();
    }

    // Update is called once per frame
    void Update ()
    {
        //Call the function to set the speed of the player
        SetSpeed ();

        ClosestTarget();
    }

    public void SetSpeed ()
    {
        //Set the player to walking speed by default
        float speed = walkSpeed;

        //If the stamina is less than or equal to 0
        if (stamina <= 0) 
        {
            //Set the player as fatigued
            isFatigued = true;
            //Set the player to walking
            speed = walkSpeed;
            //Set stamina to 0
            stamina = 0;
        }
        //If the stamina is greater than or equal to max stamina
        if(stamina >= maxStamina)
        {
            //Set the stamina to the max stamina
            stamina = maxStamina;
            //Set the player as not fatigued
            isFatigued = false;
        }
        //If the player is moving along either the x or y axis
        if (Input.GetAxis("Horizontal") !=0 || Input.GetAxis("Vertical") !=0)
         {
        //If the player is fatigued
        if(isFatigued == true)
        {
                //Set the player to walking speed
                speed = walkSpeed;
                //Player is not running
                isRunning = false;
                //Player is walking
                isWalking = true;
                //Start increasing stamina
                stamina++;
        }
        //If the player is touching the ground and the user is either pressing left shift or right shift
        else if (chCtrl.isGrounded && Input.GetKey ("left shift") || Input.GetKey ("right shift") && isFatigued == false ) 
            {
                //Set the player to running speed
                speed = runSpeed;
                //Player is running
                isRunning = true;
                //Player is not walking
                isWalking = false;
                //Start reducting stamina
                stamina--;
            }
            else
            {
                //Set the player to walking speed
                speed = walkSpeed;
                //Player is not running
                isRunning = false;
                //Player is walking
                isWalking = true;
                //Start increasing stamina
                stamina++;
            }
        }
        else
        {
            //Player is not running
            isRunning = false;
            //Player is not walking
            isWalking = false;
            //Start increasing stamina
            stamina++;
        }
        //Set the players speed to either walkSpeed or runSpeed
        chMotor.movement.maxForwardSpeed = speed;
    }

    void AddAllButtons()
    {
        //Create an array that contains all game objects tagged with 'button'
        GameObject[] buttons = GameObject.FindGameObjectsWithTag("Button"); 
        //For each of the game objects in the array
        foreach(GameObject button in buttons)
        {
            //Add the transform of the button
            AddButton(button.transform);
        }
    }

    void AddButton(Transform button)
    {
        //Add the transform of the button into the targets list
        targets.Add(button);
    }

    void ButtonCheck(Transform button)
    {   
        Vector3 dir = (button.position - transform.position).normalized;
        float direction = Vector3.Dot(dir, transform.forward);
        Debug.Log(direction);

        if(Input.GetKeyDown(KeyCode.E))
        if(direction > 0.7F && Vector3.Distance(currentTarget.position, transform.position) < 2.0F)
        {   
            print("Button has been clicked");
        }
    }

    void ClosestTarget()
    {
        foreach (Transform button in targets) 
        { 
        Vector3 diff = (button.position - transform.position); 
        float curDistance = Vector3.Distance(transform.position, button.position );
        Debug.Log(curDistance);
        if (curDistance < distance ) 
        { 
          currentTarget = button;
          distance = curDistance;
        } 
        }
    }
}

前述のように、問題は ClosestTargets 関数にあります。すべての立方体の距離を見つけていますが、ターゲット リストの最後の要素のみを選択します。

http://puu.sh/4Sh9U.png

コンソール内のすべてのキューブからの距離は引き続き表示されます。

http://puu.sh/4Shbk.png

4

2 に答える 2