玩家 1 控制玩家 2,反之亦然

发布于 2025-01-10 03:10:52 字数 3256 浏览 4 评论 0原文

当我创建房间,玩家 2 连接到房间时,我控制他,他控制我。我应该怎么办? 玩家控制器与 unity3d fps basicrigidbodypush 脚本和鼠标外观相结合。
Unity 3d 2020.3.26f 版本,开发模式关闭。视频(左键和输入字段正在创建房间,右键和输入字段正在加入房间)https://dropmefiles。 com/XPPa3。 在资源文件夹中有播放器预制件。 玩家预制件包含网格渲染器、玩家控制器脚本(如下)、角色控制器、光子视图、光子变换视图经典(同步位置和旋转打开)、刚体和光子刚体视图(全部启用)

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

public class PlayerController : MonoBehaviour
{
    PhotonView view;
    public LayerMask pushLayers;
    public bool canPush;
    [Range(0.5f, 5f)] public float strength = 1.1f;
    public float jumpHeight = 3f;
    public GameObject camera;
    public LayerMask groundMask;
    public Transform groundCheck;
    public CharacterController controller;
    public float speed = 10f;
    public float gravity = -9.8f;
    public float groundDistance = 0.4f;
    Vector3 velocity;
    bool isGrounded;
    public Transform PlayerBody;
    public float MouseSensitivity = 100f;
    float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = false;
        view = GetComponent<PhotonView>();

    }

    // Update is called once per frame
    void Update()
    {
        if (view.IsMine) {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocity.y < 0) {
            velocity.y = -7f;
        }
        if (isGrounded && Input.GetButtonDown("Jump")) {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        if(Input.GetKeyDown(KeyCode.F)) {
            CursorTrigger();
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        float mouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        camera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        PlayerBody.Rotate(Vector3.up * mouseX);
        }
    }
    private void PushRigidBodies(ControllerColliderHit hit)
    {
        if (view.IsMine) {
        Rigidbody body = hit.collider.attachedRigidbody;
        if (body == null || body.isKinematic) return;
        var bodyLayerMask = 1 << body.gameObject.layer;
        if ((bodyLayerMask & pushLayers.value) == 0) return;
        if (hit.moveDirection.y < -0.3f) return;
        Vector3 pushDir = new Vector3(hit.moveDirection.x, 0.0f, hit.moveDirection.z);
        body.AddForce(pushDir * strength, ForceMode.Impulse);
        }
    }
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (view.IsMine) {
        if (canPush) PushRigidBodies(hit);
        }
    }

    public void CursorTrigger() {
        if (view.IsMine) {
        Cursor.visible = !Cursor.visible;
        
    }

}
}```

When i create room, and player 2 connect to room, i control him, and he controls me. What should I do?
Player controller combined with unity3d fps basicrigidbodypush script and mouse look.
Unity 3d 2020.3.26f version, development mode off. Video of it (left button and input field is creating room, right button and input field is joining room)https://dropmefiles.com/XPPa3.
In resources folder there is player prefab.
Player prefab contains mesh renderer, player controller script (below), character controller, photon view, photon transform view classic (synchronize position and rotation turned on), rigidbody and photon rigidbody view (all enabled)

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

public class PlayerController : MonoBehaviour
{
    PhotonView view;
    public LayerMask pushLayers;
    public bool canPush;
    [Range(0.5f, 5f)] public float strength = 1.1f;
    public float jumpHeight = 3f;
    public GameObject camera;
    public LayerMask groundMask;
    public Transform groundCheck;
    public CharacterController controller;
    public float speed = 10f;
    public float gravity = -9.8f;
    public float groundDistance = 0.4f;
    Vector3 velocity;
    bool isGrounded;
    public Transform PlayerBody;
    public float MouseSensitivity = 100f;
    float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = false;
        view = GetComponent<PhotonView>();

    }

    // Update is called once per frame
    void Update()
    {
        if (view.IsMine) {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocity.y < 0) {
            velocity.y = -7f;
        }
        if (isGrounded && Input.GetButtonDown("Jump")) {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        if(Input.GetKeyDown(KeyCode.F)) {
            CursorTrigger();
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        float mouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        camera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        PlayerBody.Rotate(Vector3.up * mouseX);
        }
    }
    private void PushRigidBodies(ControllerColliderHit hit)
    {
        if (view.IsMine) {
        Rigidbody body = hit.collider.attachedRigidbody;
        if (body == null || body.isKinematic) return;
        var bodyLayerMask = 1 << body.gameObject.layer;
        if ((bodyLayerMask & pushLayers.value) == 0) return;
        if (hit.moveDirection.y < -0.3f) return;
        Vector3 pushDir = new Vector3(hit.moveDirection.x, 0.0f, hit.moveDirection.z);
        body.AddForce(pushDir * strength, ForceMode.Impulse);
        }
    }
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (view.IsMine) {
        if (canPush) PushRigidBodies(hit);
        }
    }

    public void CursorTrigger() {
        if (view.IsMine) {
        Cursor.visible = !Cursor.visible;
        
    }

}
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2025-01-17 03:10:52

我通常会以不同的方式来处理这个问题。请尝试以下操作:

if(!view.isMine)
{
    return;
}

然后继续您的代码。

I usually go about this in a different way. Try the following:

if(!view.isMine)
{
    return;
}

And then continue with your code afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文