Finden Sie das nächste Ziel für mich in der Einheit
Habe ich eine Spieler-Klasse in der Einheit, die alle funktioniert gut, außer für meine ClosestTarget Funktion. Die Funktion funktioniert wie ich es wollte, aber jetzt ist es immer nur picks Cube 2 (Das Letzte element in der Liste), auch wenn ich näher an einem anderen cube.
Die Klasse ist, wie gezeigt:
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;
}
}
}
}
Wie gesagt, das problem ist mit der ClosestTargets Funktion. Es zu finden ist der Abstand für jeden cube aber immer nur wählt das Letzte element in der Liste targets.
Es zeigt immer noch die Entfernung von jeder Würfel in der Konsole.
InformationsquelleAutor therealkf | 2013-10-17
Du musst angemeldet sein, um einen Kommentar abzugeben.
Meine Vermutung:
distance
ist Klasse und richtig initialisiert zuMathf.Infinity
aber es ist nie zurückgesetzt. So auf den ersten AufrufClosestTarget ()
Cube2 sein könnte, die einem am nächsten stehen Spieler wiePlayer.Update
vielleicht später aufgerufen. Sodistance
enthält einen Wert, der ist zu klein (0?) für andere Objekte eine chance haben.InformationsquelleAutor Kay
Ich diese Erweiterung Methode finden das nächstgelegene gameObject (Warnung es verwendet linq):
Der Nachteil dieser Methode ist, dass nur Objekte erfassen, die overlapwith der projizierten Kugel verpassen und Objekte, die im inneren komplett. So werde ich oft Kompliment es mit dieser rekursiven Funktion, dass die Projekte eine finit Höhe der Kugel bei verschiedenen Entfernungen.
Hinweis: während Sie similier Namen, die oben ist eine extension-Methode, und unten ist eine Instanz-Methode. Sie arbeiten zusammen hand und hand.
Dieser top-Methode funktioniert durch Angabe der Distanz ab und Ihre maximale Abstand, dann geben Sie an, wie viele Iterationen der Funktion, die Sie ausführen möchten. Basierend auf der Anzahl der Iterationen wird es cast a n Kugeln gleicher Abstand zwischen start-und stop Grenzen.
InformationsquelleAutor MichaelTaylor3D