玩家 1 控制玩家 2,反之亦然
当我创建房间,玩家 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常会以不同的方式来处理这个问题。请尝试以下操作:
然后继续您的代码。
I usually go about this in a different way. Try the following:
And then continue with your code afterwards.