Ich bin neu in der Einheit. Ziel des Codes ist es, ein Spielobjekt einen Weg zurückzuverwandeln. Ich versuche, einen Code zu machen, der den Pfad des Spielers Spielobjekt speichert, beim Drücken von 'A' wird Spielobjekt zum Ausgangspunkt & neu positioniert dann bewegt sich entsprechend Pfad gespeichert.Probleme mit der Rückverfolgung Pfad
Der Code kann die Eingabe speichern (wird mit Debug.log überprüft), wechselt aber auch zur Startposition. Aber bewegt sich nicht nach gespeichertem Pfad.
Spielobjekt bewegt sich unabhängig von der Eingabe immer von (0,0,0) nach (-0,7,0,0) (auch wenn ich keine Eingabe gebe & direkt 'A' drücken).
Ich habe vielleicht einen dummen Fehler gemacht. Entschuldigen Sie die Umstände. Hier ist der vollständige Code.
var pt1 = new Array(); //key press time
var pr1 = new Array(); //key press release
var pkh1 = new Array(); //key press horizontal component
var pkv1 = new Array(); // key press vertical component
var level : int = 0;
var i : int = 0; // i is car number of this script
var t1 : float = 0;
var j1 : int = 0;
var downUp;
var heldUp;
var upUp;
var downDown;
var heldDown;
var upDown;
var downLeft;
var heldLeft;
var upLeft;
var downRight;
var heldRight;
var upRight;
var downSpace;
var heldSpace;
var upSpace;
var stopwatch : System.Diagnostics.Stopwatch = new System.Diagnostics.Stopwatch();
var stopwatch1 : System.Diagnostics.Stopwatch = new System.Diagnostics.Stopwatch();
var l = true;
var speed : float = 6f; // The speed that the player will move at.
private var movement : Vector3; // The vector to store the direction of the player's movement.
private var playerRigidbody : Rigidbody; // Reference to the player's rigidbody.
function Awake() {
playerRigidbody = GetComponent (Rigidbody);
}
function move (h : float, v : float)
{
// Set the movement vector based on the axis input.
movement.Set (h, 0f, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);
}
function Start() {
stopwatch.Start();
}
function FixedUpdate()
{
// Store the input axes.
var h : float = Input.GetAxisRaw ("Horizontal");
var v : float = Input.GetAxisRaw ("Vertical");
// Move the player around the scene.
move (h, v);
}
function Update()
{
if(l)
{
//for current player car
downUp = Input.GetKeyDown(KeyCode.UpArrow);
heldUp = Input.GetKey(KeyCode.UpArrow);
upUp = Input.GetKeyUp(KeyCode.UpArrow);
if(downUp)
{
pt1[j1] = stopwatch.ElapsedMilliseconds;
pkh1[j1] = Input.GetAxisRaw ("Horizontal");
pkv1[j1] = Input.GetAxisRaw ("Vertical");
}
if(heldUp)
{
}
if(upUp)
{
pr1[j1] = stopwatch.ElapsedMilliseconds;
j1++;
}
downDown = Input.GetKeyDown(KeyCode.DownArrow);
heldDown = Input.GetKey(KeyCode.DownArrow);
upDown = Input.GetKeyUp(KeyCode.DownArrow);
if(downDown) {
pt1[j1] = stopwatch.ElapsedMilliseconds;
pkh1[j1] = Input.GetAxisRaw ("Horizontal");
pkv1[j1] = Input.GetAxisRaw ("Vertical");
}
if(heldDown) {
}
if(upDown) {
pr1[j1]= stopwatch.ElapsedMilliseconds;
j1++;
}
downLeft = Input.GetKeyDown(KeyCode.LeftArrow);
heldLeft = Input.GetKey(KeyCode.LeftArrow);
upLeft = Input.GetKeyUp(KeyCode.LeftArrow);
if(downLeft) {
pt1[j1] = stopwatch.ElapsedMilliseconds;
pkh1[j1] = Input.GetAxisRaw ("Horizontal");
pkv1[j1] = Input.GetAxisRaw ("Vertical");
}
if(heldLeft) {
}
if(upLeft) {
pr1[j1] = stopwatch.ElapsedMilliseconds;
j1++;
}
downRight = Input.GetKeyDown(KeyCode.RightArrow);
heldRight = Input.GetKey(KeyCode.RightArrow);
upRight = Input.GetKeyUp(KeyCode.RightArrow);
if(downRight) {
pt1[j1] = stopwatch.ElapsedMilliseconds;
pkh1[j1] = Input.GetAxisRaw ("Horizontal");
pkv1[j1] = Input.GetAxisRaw ("Vertical");
}
if(heldRight) {
}
if(upRight) {
pr1[j1] = stopwatch.ElapsedMilliseconds;
j1++;
}
downSpace = Input.GetKeyDown(KeyCode.A);
heldSpace = Input.GetKey(KeyCode.A);
upSpace = Input.GetKeyUp(KeyCode.A);
if(downSpace || heldSpace)
{
transform.position = Vector3(0f,0f,0f);
stopwatch.Stop();
l = false;
stopwatch1.Start();
j1 = 0;
}
}
else {
t1 = stopwatch1.ElapsedMilliseconds;
Debug.Log(j1);
Debug.Log(transform.position);
if(pt1[j1] <= t1) {
while(pr1[j1] >= t1) {
move(pkh1[j1], pkv1[j1]);
}
j1++;
}
}
}
Ich empfehle definitiv, diesen Code zu wiederholen. Ich sehe keinen Grund für all diese Bools. Schreiben Sie einfach 'if (Input.GetKey (...)) {...}' usw. direkt. Für das Debugging empfehle ich, Ihr 'pkh1' und' pkv1' öffentlich zu machen, damit Sie seine Daten zur Laufzeit sehen können. –
Ich bin nicht ganz sicher, ob ich Ihre Idee richtig verstanden habe, aber ich denke, Sie versuchen, der Eingabe zu folgen, die Sie mit 'Input.GetAxisRaw' erhalten, die entweder -1, 0 oder 1 ist, also keine tatsächlichen Positionen. –