2016-03-24 21 views
1

Bir kitap üzerinde çalışıyorum ve bir sorunla karşılaştım. Bu oyuncu fare ile döndürmek ve klavye ile hareket ettirmek bir yukarı aşağı çekim oyunudur. Sorun, fare ya da klavyenin ayarlanmış hareketinin görüntü titreştiğini test etmektir. Ok tuşlarına basarsam bir daire içinde hareket edersek, anahtarı daha geniş tuttuğumda daire daha geniş olur. Aşağıda çalıştığım komut dosyası.Hareket birlik başladığında karakter titreşir

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; 
    } 
} 

cevap

1

Kodunuzu bir süre okudum ve yanlış bir şey bulamadım. Bu yüzden kendim test ettim ve mükemmel çalışıyor.

Bu yüzden sahnedeki bir sorun var. Örneğin, oyuncu nesnesini, eksenlerinizi döndüren bir nesnenin çocuğu olabilirsiniz. Bu, sanırım sorunlara neden olabilir.

Yeni ve boş bir sahne hazırlayın. Yeni bir GameObject (örneğin bir 3D küp veya bir 2D sprite) ekleyin ve buna PlayerBehaviour atayın. Şimdi test et: Mükemmel çalışmalı.

+0

Yardımlarınız için şimdiden teşekkürler. –

İlgili konular