2016-03-24 7 views
1

Ich arbeite durch ein Buch und bin zu einem Problem gekommen. Es ist ein Top-Down-Shooter, bei dem der Spieler mit der Maus rotiert und sich mit der Tastatur bewegt. Das Problem ist, dass beim Testen, ob die Maus oder die Tastatur die Bewegung auslösen, das Bild vibriert. Wenn ich die Pfeiltasten drücke, bewegt es sich in einem Kreis, je länger ich die Taste halte, desto breiter ist der Kreis. Unten ist das Skript, mit dem ich arbeite.Charakter vibriert, wenn Bewegung Einheit beginnt

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

public class PlayerBehaviour : MonoBehaviour 
{ 
    //movement modifier applied to directional movement 
    public float playerSpeed = 2.0f; 

    //current player speed 
    private float currentSpeed = 0.0f; 

    /* 
    * Allows us to have multiple inputs and supports keyboard, 
    * joystick, etc. 
    */ 
    public List<KeyCode> upButton; 
    public List<KeyCode> downButton; 
    public List<KeyCode> leftButton; 
    public List<KeyCode> rightButton; 

    //last movement made 
    private Vector3 lastMovement = new Vector3(); 

    // Update is called once per frame 
    void Update() 
    { 
     //rotates ship to face mouse 
     Rotation(); 
     //moves ship 
     Movement(); 
    } 

    void Rotation() 
    { 
     //finds mouse in relation to player location 
     Vector3 worldPos = Input.mousePosition; 
     worldPos = Camera.main.ScreenToWorldPoint(worldPos); 

     /* 
     get x and y screen positions 
     */ 
     float dx = this.transform.position.x - worldPos.x; 
     float dy = this.transform.position.y - worldPos.y; 

     //find the angle between objects 
     float angle = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg; 

     /* 
     * The transform's rotation property uses a Quaternion, 
     * so we need to convert the angle in a Vector 
     * (The Z axis is for rotation for 2D). 
     */ 
     Quaternion rot = Quaternion.Euler(new Vector3(0, 0, angle + 90)); 
     // Assign the ship's rotation 
     this.transform.rotation = rot; 
    } 

    // Will move the player based off of keys pressed 
    void Movement() 
    { 
     // The movement that needs to occur this frame 
     Vector3 movement = new Vector3(); 
     // Check for input 
     movement += MoveIfPressed(upButton, Vector3.up); 
     movement += MoveIfPressed(downButton, Vector3.down); 
     movement += MoveIfPressed(leftButton, Vector3.left); 
     movement += MoveIfPressed(rightButton, Vector3.right); 
     /* 
     * If we pressed multiple buttons, make sure we're only 
     * moving the same length. 
     */ 
     movement.Normalize(); 
     // Check if we pressed anything 
     if (movement.magnitude > 0) 
     { 
      // If we did, move in that direction 
      currentSpeed = playerSpeed; 
      this.transform.Translate(movement * Time.deltaTime * playerSpeed, Space.World); 
      lastMovement = movement; 
     } 
     else 
     { 
      // Otherwise, move in the direction we were going 
      this.transform.Translate(lastMovement * Time.deltaTime * currentSpeed, Space.World); 
      // Slow down over time 
      currentSpeed *= .9f; 
     } 
    } 

    /* 
    * Will return the movement if any of the keys are pressed, 
    * otherwise it will return (0,0,0) 
    */ 
    Vector3 MoveIfPressed(List<KeyCode> keyList, Vector3 Movement) 
    { 
     // Check each key in our list 
     foreach (KeyCode element in keyList) 
     { 
      if (Input.GetKey(element)) 
      { 
       /* 
       * It was pressed so we leave the function 
       * with the movement applied. 
       */ 
       return Movement; 
      } 
     } 
     // None of the keys were pressed, so don't need to move 
     return Vector3.zero; 
    } 
} 

Antwort

1

Ich studierte Ihren Code für eine Weile und konnte nichts falsch finden. Also habe ich es selbst getestet und es funktioniert perfekt.

Also ich nehme an, dass etwas mit deiner Szene nicht stimmt. Sie könnten zum Beispiel Ihr Spielerobjekt als Kind eines Objekts haben, das Ihre Achsen rotiert: Dies würde Probleme verursachen, nehme ich an.

Haben Sie eine neue, leere Szene. Fügen Sie ein neues GameObject hinzu (z. B. einen 3D-Würfel oder ein 2D-Sprite) und weisen Sie ihm PlayerBehaviour zu. Jetzt test: es sollte perfekt funktionieren.

+0

Vielen Dank für die Hilfe, es funktioniert jetzt wie erwartet. –

Verwandte Themen