【问题标题】:Unity photon View统一光子视图
【发布时间】:2019-12-17 23:18:41
【问题描述】:

所以我在我的第一个在线统一游戏中使用光子,但我已经遇到了问题。我的角色移动脚本运行良好,同步良好等。但是,当我尝试执行固定到玩家的相机时,它会跟随它,但第二个玩家看起来像是在移动(但是变换位置没有移动)。 这是我的 PlayerInitializer 脚本,用于生成我的播放器并附加相机:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class PlayerInitializer : MonoBehaviour
{
    // Start is called before the first frame update
    public Camera mainCamera;
    void Start()
    {
        StartCoroutine(CreatePlayer());
    }

    IEnumerator CreatePlayer()
    {
        Debug.Log("Creation du joueur");
        GameObject MyPlayer = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), Vector3.zero, Quaternion.identity) as GameObject;
        mainCamera.GetComponent<Following_Camera>().player = MyPlayer.transform;
        yield return new WaitForSeconds(1);
    }
}

这是 PlayerMovement 脚本(一个基本脚本):

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator animator;
    Vector2 movement;
    PhotonView view;
    private void Start()
    {
        view = GetComponent<PhotonView>();
    }
    private void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
    }
    void FixedUpdate()
    {
        if (view.IsMine)
        {
            rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        }
    }
}

然后是我的相机脚本:

using UnityEngine;
using System.Collections;

public class Following_Camera : MonoBehaviour
{
    public Transform player;
    private Vector3 offset;

    void Start()
    {
        offset = new Vector3(0,0,-10);

    }

    void LateUpdate()
    {
        transform.position = player.transform.position + offset ;
    }
}

Video of the problem

【问题讨论】:

  • 私人视频,无法访问
  • 它在我认为

标签: unity3d


【解决方案1】:

Z 可能会在

中发生变化
  player.transform.position

也许

transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, -10)

足以让相机停止转圈

【讨论】:

  • 问题似乎是一样的,但我在想也许它可能是动画师精灵的位置机会。但是游戏对象没有移动。
  • 呵呵,一问,你确定z分量是朝上的吗?通常是y轴朝上,意思是motion.y = Input.GetAxisRaw("Vertical");宁愿是motion.z,所以你会保持Y不变并通过X和Z移动
【解决方案2】:

好的,所以如果有人遇到同样的问题,请注意您是否在哪里使用 3D 对象进行测试。问题是我在字体中使用了 3D 框,所以 2D 运动就像我在看他身边的另一个精灵一样。这就是为什么变换没有改变,但视图却改变了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 2011-05-02
    • 2021-07-14
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多