Unity移动玩家腿部多人游戏

问题描述 投票:1回答:1

我目前正在开发一款游戏,我遇到以下情况:

我有一个播放器预制游戏对象,附带一个脚本(见下文)。我已经设置了网络管理器,并且我在“服务”下设置了一个帐户,以便能够使用多人游戏方面。

我已经设置了基础知识,以便玩家可以生成并且多人游戏可以正常工作。玩家能够移动,我在每个构建会话中看到其他玩家的移动。

我有一大堆代码,当玩家“走路”时(如果按下A,W,S或D键,我称之为“CmdWalk()”)。

基本上CmdWalk()使它改变我的玩家的腿旋转,使它看起来像是在行走。 (我不是动画,所以这是我所知道的唯一方式)。

问题是只有本地玩家才能看到他们的玩家“走路”,其他玩家在网上看不到动作。我不确定我做错了什么,有人可以帮忙。

以下是我对播放器的完整脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PlayerController : NetworkBehaviour
{
    public float speedH = 2.0f;
    private float yaw = 0.0f;

    public float WalkingTime; //timer for walking animation
    public GameObject PlayerLeftLeg;
    public GameObject PlayerRightLeg;

    private float PlayerStatMenuTimer;
    public GameObject PlayerStatsMenu;

    // Update is called once per frame
    void Update () 
    {

        if (!isLocalPlayer)
        {
            return;
        }

        //keep track of time for player stat menu
        //if not here than menua will show and hide like a thousand times when pressed once due to update reading code per frame
        PlayerStatMenuTimer = PlayerStatMenuTimer + 1 * Time.deltaTime;

        //moving player left right forward backward
        var x = Input.GetAxis ("Horizontal") * Time.deltaTime * 50.0f;
        var z = Input.GetAxis ("Vertical") * Time.deltaTime * 50.0f;
        transform.Translate (x, 0, z);

        //rotating player or "Looking"
        yaw += speedH * Input.GetAxis ("Mouse X");
        transform.eulerAngles = new Vector3 (0.0f, yaw, 0.0f);

        //if player is using WASD to move then do leg moving animation
        //if not moving then set legs to be still and reset in standing position
        //FYI:  "transform.TransformVector(1,0,0)" was used instead of "Vector3.forward" was because
        //   vector3.forward is local space, so when i rotate player the sense of "forward" also changes, thus i needed
        //  a code that uses the world space, thus i used "transform.TransformVector(1,0,0)"
        if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D))
        {
            CmdWalk ();
        }
        else
        {
            //if player not walking then reset
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis (0, Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis (0, Vector3.forward);
            WalkingTime = 0;
        }

        //get hidden mouse pointer back and unlock
        if (Input.GetKey (KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.None;
        }

        //opens and closes stat menu
        if (Input.GetKey (KeyCode.Return) && (PlayerStatMenuTimer >= 1) && (PlayerStatsMenu.activeSelf == false)) 
        {
            Cursor.lockState = CursorLockMode.None;
            PlayerStatsMenu.SetActive (true);
            PlayerStatMenuTimer = 0;

            //call the script "GetplayerStats" and call function "retrieceplayerstats"
            var GetStats = GetComponent<GetPlayerStats> ();
            GetStats.RetrievePlayerStats ();

        }
        else if (Input.GetKey (KeyCode.Return) && PlayerStatMenuTimer >= 1 && PlayerStatsMenu == true) 
        {
            Cursor.lockState = CursorLockMode.Locked;
            PlayerStatsMenu.SetActive (false);
            PlayerStatMenuTimer = 0;
        }
    }

    private void Awake ()
    {
        //this code locks mouse onto center of window
        //Screen.lockCursor = true;
        Cursor.lockState = CursorLockMode.Locked;
    }

    [Command]
    void CmdWalk ()
    {
        //timer
        WalkingTime += Time.deltaTime;

        //right leg stepping forward
        if (WalkingTime > 0 && WalkingTime < .4)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis (PlayerRightLeg.transform.rotation.x - (60 * WalkingTime), transform.TransformVector (1, 0, 0));
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis (PlayerLeftLeg.transform.rotation.x + (60 * WalkingTime), transform.TransformVector (1, 0, 0));
        }

        //left leg stepping forward
        if (WalkingTime >.4 && WalkingTime < 1.2)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis (PlayerRightLeg.transform.rotation.x + (60 * (WalkingTime - .8f)), transform.TransformVector (1, 0, 0));
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis (PlayerLeftLeg.transform.rotation.x - (60 * (WalkingTime - .8f)), transform.TransformVector (1, 0, 0));
        }

        //right leg stepping forward
        if (WalkingTime > 1.2 && WalkingTime < 1.59)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis (PlayerRightLeg.transform.rotation.x - (60 * (WalkingTime - 1.6f)), transform.TransformVector (1, 0, 0));
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis (PlayerLeftLeg.transform.rotation.x + (60 * (WalkingTime - 1.6f)), transform.TransformVector (1, 0, 0));
        }

        //resetting
        if (WalkingTime > 1.6)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis (0, Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis (0, Vector3.forward);
            WalkingTime = 0;
        }
    }
}

很抱歉代码的数量,但唯一需要注意的部分是“A”“W”“S”“D”和“Void CmdWalk()”的“IF语句”

谢谢。

c# unity3d game-engine
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.