admin管理员组文章数量:1291393
I am making a multiplayer game using photon and everything was working fine until I added photon, and the player is randomly falling.
A character controller and a rigidbody component is attached to the player and also a photon view, photon transform view and photon rigidbody view is attached.
Here are the scripts, RoomManager, player setup and player movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class RoomManager : MonoBehaviourPunCallbacks
{
public GameObject player;
[Space]
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
Debug.Log("Connecting");
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Connected To Server");
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
base.OnJoinedLobby();
Debug.Log("We Are In The Lobby");
PhotonNetwork.JoinOrCreateRoom("test", null, null);
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
Debug.Log("We're Connected and in a room now");
GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
_player.GetComponent<PlayerSetup>().IsLocalPlayer();
}
}
using Photon.Pun;
using UnityEngine;
public class PlayerSetup : MonoBehaviour, IPunInstantiateMagicCallback
{
public PlayerMovement movement;
public GameObject camera;
public void IsLocalPlayer()
{
PhotonView photonView = GetComponent<PhotonView>();
Debug.Log($"IsLocalPlayer called for: {gameObject.name}, IsMine: {photonView.IsMine}");
if (photonView.IsMine) // Check if this player is controlled by the local client
{
Debug.Log($"Local player: {gameObject.name}");
movement.enable = true; // Enable movement for the local player
if (camera != null)
{
camera.SetActive(true); // Activate the camera for the local player
Debug.Log($"Camera activated for: {gameObject.name}");
}
else
{
Debug.LogWarning($"Camera is not assigned for: {gameObject.name}");
}
}
else
{
Debug.Log($"Remote player: {gameObject.name}");
movement.enable = false; // Disable movement for other players
if (camera != null)
{
camera.SetActive(false); // Optionally deactivate the camera for other players
Debug.Log($"Camera deactivated for: {gameObject.name}");
}
else
{
Debug.LogWarning($"Camera is not assigned for remote player: {gameObject.name}");
}
}
}
public void OnPhotonInstantiate(PhotonMessageInfo info)
{
// This method is called on all clients when the object is instantiated
IsLocalPlayer(); // Call the method to set up the player
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
public Camera playerCamera; // Ensure this is assigned in the Inspector
public float walkSpeed = 6f;
public float runSpeed = 12f;
public float jumpPower = 7f;
public float gravity = 10f;
public float lookSpeed = 2f;
public float lookXLimit = 45f;
public float defaultHeight = 2f;
public float crouchHeight = 1f;
public float crouchSpeed = 3f;
public bool enable = true;
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0;
private CharacterController characterController;
private bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
if (!enable) return; // Prevent movement if not enabled
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical");
float curSpeedY = (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal");
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (characterController.isGrounded)
{
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpPower;
}
else
{
moveDirection.y = 0; // Reset vertical movement when grounded
}
}
else
{
moveDirection.y -= gravity * Time.deltaTime; // Apply gravity when not grounded
}
// Crouch logic
if (Input.GetKey(KeyCode.R))
{
characterController.height = crouchHeight;
walkSpeed = crouchSpeed;
runSpeed = crouchSpeed;
}
else
{
characterController.height = defaultHeight;
walkSpeed = 6f;
runSpeed = 12f;
}
characterController.Move(moveDirection * Time.deltaTime);
// Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
本文标签: cMy player is randomly falling in void from the surfaceStack Overflow
版权声明:本文标题:c# - My player is randomly falling in void from the surface - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741510813a2382594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论