【发布时间】: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 ;
}
}
【问题讨论】:
-
私人视频,无法访问
-
它在我认为
标签: unity3d