【问题标题】:Character vibrates when movement starts unity当运动开始统一时,角色会振动
【发布时间】:2016-03-24 15:11:57
【问题描述】:

我正在阅读一本书,但遇到了一个问题。这是一款自上而下的射击游戏,玩家可以用鼠标旋转并用键盘移动。问题是在测试鼠标或键盘是否引起图像振动时。如果我按箭头键,它会在一个圆圈中移动,我按住键的时间越长,圆圈就越宽。下面是我正在使用的脚本。

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

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    我研究了你的代码一段时间,没有发现任何问题。所以我自己测试了一下,效果很好。

    所以我想你的场景有问题。例如,您可能让您的播放器对象成为某个旋转轴的对象的子对象:我想这会导致问题。

    有一个新的、空旷的场景。添加一个新的游戏对象(例如 3D 立方体,或 2D 精灵)并将 PlayerBehaviour 分配给它。现在测试:它应该可以完美运行。

    【讨论】:

    • 感谢您的帮助,它现在按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多