Я использую многопользовательский учебник, чтобы добавить в мою игру. Этот парень делает свое движение go к серверу, но дело в том, что мое движение полностью отличается от его. Вот его:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private void FixedUpdate()
{
SendInputToServer();
}
private void SendInputToServer()
{
bool[] _inputs = new bool[]
{
Input.GetKey(KeyCode.W),
Input.GetKey(KeyCode.S),
Input.GetKey(KeyCode.A),
Input.GetKey(KeyCode.D),
};
ClientSend.PlayerMovement(_inputs);
}
}
Мой сценарий движения такой:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerMovement : MonoBehaviour
{
public Transform playerCam;
public Transform orientation;
private Rigidbody rb;
private PlayerStats stats;
public ShieldBar shieldBar;
public HealthBar healthBar;
private float xRotation;
private float sensitivity = 50f;
private float sensMultiplier = 1f;
public float moveSpeed = 4500;
public float maxSpeed = 20;
public bool grounded;
public LayerMask whatIsGround;
public float counterMovement = 0.175f;
private float threshold = 0.01f;
public float maxSlopeAngle = 35f;
private Vector3 crouchScale = new Vector3(1, 0.5f, 1);
private Vector3 playerScale;
public float slideForce = 400;
public float slideCounterMovement = 0.2f;
private bool readyToJump = true;
private float jumpCooldown = 0.25f;
public float jumpForce = 550f;
float x, y;
bool jumping, sprinting, crouching;
private Vector3 normalVector = Vector3.up;
private Vector3 wallNormalVector;
void Awake()
{
rb = GetComponent<Rigidbody>();
stats = GetComponent<PlayerStats>();
}
void Start()
{
playerScale = transform.localScale;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void FixedUpdate()
{
Movement();
}
private void Update()
{
MyInput();
Look();
}
private void MyInput()
{
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
jumping = Input.GetKey(KeyCode.Space);
crouching = Input.GetKey(KeyCode.C);
if (Input.GetKeyDown(KeyCode.C))
StartCrouch();
if (Input.GetKeyUp(KeyCode.C))
StopCrouch();
}
private void StartCrouch()
{
transform.localScale = crouchScale;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
if (rb.velocity.magnitude > 0.5f)
if (grounded)
rb.AddForce(orientation.transform.forward * slideForce);
}
private void StopCrouch()
{
transform.localScale = playerScale;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
}
private void Movement()
{
rb.AddForce(Vector3.down * Time.deltaTime * 10);
Vector2 mag = FindVelRelativeToLook();
float xMag = mag.x, yMag = mag.y;
CounterMovement(x, y, mag);
if (readyToJump && jumping)
Jump();
float maxSpeed = this.maxSpeed;
if (crouching && grounded && readyToJump)
{
rb.AddForce(Vector3.down * Time.deltaTime * 3000);
return;
}
if (x > 0 && xMag > maxSpeed) x = 0;
if (x < 0 && xMag < -maxSpeed) x = 0;
if (y > 0 && yMag > maxSpeed) y = 0;
if (y < 0 && yMag < -maxSpeed) y = 0;
float multiplier = 1f, multiplierV = 1f;
if (!grounded)
{
multiplier = 0.5f;
multiplierV = 0.5f;
}
if (grounded && crouching)
multiplierV = 0f;
rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
}
private void Jump()
{
if (grounded && readyToJump)
{
readyToJump = false;
rb.AddForce(Vector2.up * jumpForce * 1.5f);
rb.AddForce(normalVector * jumpForce * 0.5f);
Vector3 vel = rb.velocity;
if (rb.velocity.y < 0.5f)
rb.velocity = new Vector3(vel.x, 0, vel.z);
else if (rb.velocity.y > 0)
rb.velocity = new Vector3(vel.x, vel.y / 2, vel.z);
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void ResetJump()
{
readyToJump = true;
}
private float desiredX;
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
//Find current look rotation
Vector3 rot = playerCam.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
//Rotate, and also make sure we dont over- or under-rotate.
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
//Perform the rotations
playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
private void CounterMovement(float x, float y, Vector2 mag)
{
if (!grounded || jumping)
return;
if (crouching)
{
rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
return;
}
if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x > threshold && x < 0))
rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x * counterMovement);
if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y > threshold && y < 0))
rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y * counterMovement);
if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed)
{
float fallspeed = rb.velocity.y;
Vector3 n = rb.velocity.normalized * maxSpeed;
rb.velocity = new Vector3(n.x, fallspeed, n.z);
}
}
public Vector2 FindVelRelativeToLook()
{
float lookAngle = orientation.transform.eulerAngles.y;
float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
float u = Mathf.DeltaAngle(lookAngle, moveAngle);
float v = 90 - u;
float magnitue = rb.velocity.magnitude;
float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
return new Vector2(xMag, yMag);
}
private bool IsFloor(Vector3 v)
{
float angle = Vector3.Angle(Vector3.up, v);
return angle < maxSlopeAngle;
}
private bool cancellingGrounded;
private void OnCollisionStay(Collision other)
{
int layer = other.gameObject.layer;
if (whatIsGround != (whatIsGround | (1 << layer)))
return;
for (int i = 0; i < other.contactCount; i++)
{
Vector3 normal = other.contacts[i].normal;
if (IsFloor(normal))
{
grounded = true;
cancellingGrounded = false;
normalVector = normal;
CancelInvoke(nameof(StopGrounded));
}
}
float delay = 3f;
if (!cancellingGrounded)
{
cancellingGrounded = true;
Invoke(nameof(StopGrounded), Time.deltaTime * delay);
}
}
private void StopGrounded()
{
grounded = false;
}
private void OnTriggerEnter(Collider collision)
{
if (collision.CompareTag("Shield"))
{
if (stats.currentShield == stats.maxShield)
{
StartCoroutine(FullShield());
//Debug.Log("You have full shield!");
}
else if (stats.currentShield < stats.maxShield)
{
FindObjectOfType<AudioManager>().Play("ShieldPickup");
Destroy(collision.gameObject);
stats.currentShield += 5f;
shieldBar.SetShield(stats.currentShield);
}
}
if (collision.CompareTag("Health"))
{
if (stats.currentHealth == 100f)
{
StartCoroutine(FullHealth());
//Debug.Log("You have full HP!");
}
else if (stats.currentHealth < 100f)
{
FindObjectOfType<AudioManager>().Play("HealthPickup");
Destroy(collision.gameObject);
stats.currentHealth += 5f;
healthBar.SetHealth(stats.currentHealth);
}
}
}
IEnumerator FullShield()
{
stats.fullShield.SetActive(true);
yield return new WaitForSeconds(3);
stats.fullShield.SetActive(false);
}
IEnumerator FullHealth()
{
stats.fullHealth.SetActive(true);
yield return new WaitForSeconds(3);
stats.fullHealth.SetActive(false);
}
}
В основном я пытаюсь поместить все из MyInput () в SendInputToServer (), но это должно быть бул. Вот ссылка на видео, если кому-то нужна дополнительная информация!
https://www.youtube.com/watch?v=_h6Ta-vxAzQ
Извините, я знаю, что кода много.