Мой игрок случайно падает в пустоте с поверхностиC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Мой игрок случайно падает в пустоте с поверхности

Сообщение Anonymous »

Я делаю многопользовательскую игру, используя фотон, и все работало нормально, пока я не добавил фотон, а игрок случайным образом падает. Игрок, а также вид с фотоном, вид с фотонным преобразованием и изображение фотонного жесткого тела. class = "lang-cs prettyprint-override">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().IsLocalPlayer();
}
}

using Photon.Pun;
using UnityEngine;

public class PlayerSetup : MonoBehaviour, IPunInstantiateMagicCallback
{
public PlayerMovement movement;
public GameObject camera;

public void IsLocalPlayer()
{
PhotonView photonView = GetComponent();
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();
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);
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/794 ... he-surface
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»