Untitled
unknown
plain_text
2 years ago
5.3 kB
14
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Alteruna;
using UI_InputSystem.Base;
public class MultiplayerMovement : AttributesSync
{
// The speed that the player will move at.
public float speed = 6f;
public static MultiplayerMovement LocalPlayer;
// The vector to store the direction of the player's movement.
Vector3 movement;
// Reference to the animator component.
Animator anim;
// Reference to the player's rigidbody.
Rigidbody playerRigidbody;
// A layer mask so that a ray can be cast just at gameobjects on the floor layer.
int floorMask;
// The length of the ray from the camera into the scene.
float camRayLength = 100f;
int holaCount = 0;
public float rotationOffset;
private Alteruna.Avatar _avatar;
public Camera myCamera;
private void OnRoomJoined(Multiplayer multiplayer, Room room, User user)
{
Camera mainCamera = Camera.main;
// Disable the main camera if found
if (mainCamera != null)
{
mainCamera.enabled = false;
}
}
void Awake()
{
// Create a layer mask for the floor layer.
floorMask = LayerMask.GetMask("Floor");
// Set up references.
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void Start()
{
_avatar = GetComponent<Alteruna.Avatar>();
{
if (_avatar.IsOwner)
//Camera.main.GetComponent<CameraFollowMultiplayer>().SetTarget(gameObject.transform);
gameObject.name = "localPlayer (Host)";
LocalPlayer = this;
}
}
private void LateUpdate()
{
// Check if the main camera is not null
if (!_avatar.IsOwner)
return;
if (Camera.main != null)
{
// Attempt to get the CameraFollowMultiplayer component
CameraFollowMultiplayer cameraFollowScript = Camera.main.GetComponent<CameraFollowMultiplayer>();
// Check if the component is not null
if (cameraFollowScript != null)
{
// Set the target for the camera
cameraFollowScript.SetTarget(gameObject.transform);
}
else
{
// Handle the case where CameraFollowMultiplayer component is not found on the main camera.
Debug.Log("CameraFollowMultiplayer component not found on the main camera.");
}
}
else
{
// Handle the case where Camera.main is null (e.g., log a warning).
Debug.LogError("Main camera not found.");
}
}
void FixedUpdate()
{
if (!_avatar.IsOwner || (TextChatSynchronizable.Instance != null && TextChatSynchronizable.Instance.isChatActive))
return;
float h = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisHorizontal(JoyStickAction.Movement) : 0f;
float v = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisVertical(JoyStickAction.Movement) : 0f;
// Move the player around the scene.
Move(h, v);
// Turn the player.
float horizontalInput = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisHorizontal(JoyStickAction.CameraLook) : 0f;
float verticalInput = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisVertical(JoyStickAction.CameraLook) : 0f;
BroadcastRemoteMethod("Turning");
bool walking = h != 0f || v != 0f;
BroadcastRemoteMethod(nameof(AnimatingIsWalking), walking);
}
[SynchronizableMethod]
void AnimatingIsWalking(bool walking)
{
anim.SetBool("IsWalking", walking);
}
void Move(float h, float v)
{
if (!_avatar.IsOwner || (TextChatSynchronizable.Instance != null && TextChatSynchronizable.Instance.isChatActive))
return;
// Set the movement vector based on the axis input.
movement.Set(h, 0f, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to its current position plus the movement.
playerRigidbody.MovePosition(transform.position + movement);
}
[SynchronizableMethod]
void Turning()
{
float horizontalInput = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisHorizontal(JoyStickAction.CameraLook) : 0f;
float verticalInput = UIInputSystem.ME != null ? UIInputSystem.ME.GetAxisVertical(JoyStickAction.CameraLook) : 0f;
Vector3 inputVector = new Vector3(horizontalInput, 0f, verticalInput);
// If there is any input (magnitude of the input vector is greater than a small threshold).
if (inputVector.sqrMagnitude > 0.01f)
{
// Create a quaternion (rotation) based on the input vector.
Quaternion newRotation = Quaternion.LookRotation(inputVector.normalized);
MultiplayerShooting.Instance.StartShoot();
Debug.LogWarning("StartShooting");
// Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation(newRotation);
}
}
}
Editor is loading...
Leave a Comment