Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当运动开始统一时,角色会振动_C#_Visual Studio_Unity3d - Fatal编程技术网

C# 当运动开始统一时,角色会振动

C# 当运动开始统一时,角色会振动,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我正在翻阅一本书,遇到了一个问题。这是一个自上而下的射击游戏,玩家用鼠标旋转,用键盘移动。问题是,当测试鼠标或键盘是否引起移动时,图像会振动。如果我按箭头键,它会在一个圆圈中移动,我按住箭头键的时间越长,圆圈就越宽。下面是我正在使用的脚本 using UnityEngine; using System.Collections; using System.Collections.Generic; public class PlayerBehaviour : MonoBehaviour {

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

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;
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类玩家行为:单一行为
{
//应用于定向运动的运动修改器
公共花车播放机速度=2.0f;
//当前播放器速度
专用浮动电流速度=0.0f;
/*
*允许我们有多个输入并支持键盘,
*操纵杆等。
*/
公共列表向上按钮;
公共列表下拉按钮;
公共列表左键;
公共列表右键;
//最后一个动作
私有向量3 lastMovement=新向量3();
//每帧调用一次更新
无效更新()
{
//旋转“船到面”鼠标
旋转();
//开船
运动();
}
空位旋转()
{
//查找与玩家位置相关的鼠标
Vector3 worldPos=输入。鼠标位置;
worldPos=摄像头.main.ScreenToWorldPoint(worldPos);
/*
获取x和y屏幕位置
*/
float dx=this.transform.position.x-worldPos.x;
float dy=this.transform.position.y-worldPos.y;
//找出物体之间的角度
浮动角度=数学Atan2(dy,dx)*数学Rad2Deg;
/*
*变换的旋转属性使用四元数,
*所以我们需要转换向量中的角度
*(Z轴用于二维旋转)。
*/
四元数rot=Quaternion.Euler(新向量3(0,0,角度+90));
//指定船舶的旋转方向
this.transform.rotation=rot;
}
//将根据按下的键数移动播放器
空位移动()
{
//需要在此帧中发生的移动
Vector3移动=新Vector3();
//检查输入
移动+=移动按下(向上按钮,矢量3.向上);
移动+=移动按下(向下按钮,矢量3.向下);
movement+=MoveIfPressed(左键,矢量3.左);
移动+=移动按下(右按钮,矢量3.右);
/*
*如果我们按了多个按钮,确保我们只
*移动相同的长度。
*/
运动。正常化();
//检查我们是否按了什么
如果(移动幅度>0)
{
//如果我们这样做了,就朝那个方向走
当前速度=播放器速度;
这个.transform.Translate(运动*Time.deltaTime*playerSpeed,Space.World);
最后的运动=运动;
}
其他的
{
//否则,朝我们要去的方向走
this.transform.Translate(lastmotation*Time.deltaTime*currentSpeed,Space.World);
//慢慢来
电流速度*=0.9f;
}
}
/*
*如果按下任何键,将返回移动,
*否则它将返回(0,0,0)
*/
Vector3移动如果按下(列表键列表,Vector3移动)
{
//检查列表中的每个键
foreach(键列表中的键代码元素)
{
if(Input.GetKey(元素))
{
/*
*它被按下了,所以我们离开了功能
*随着运动的进行。
*/
回归运动;
}
}
//没有按任何键,因此不需要移动
返回向量3.0;
}
}

我研究了您的代码一段时间,没有发现任何错误。所以我自己测试了一下,效果非常好

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


有一个新的,空的场景。添加一个新的游戏对象(例如3D立方体或2D精灵)并为其指定PlayerBehaviour。现在测试:它应该可以很好地工作。

谢谢您的帮助,它现在可以正常工作了。