103 lines
2.3 KiB
C#
103 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : NetworkBehaviour
|
|
{
|
|
public PredictedRigidbody2D rb;
|
|
[SyncVar]
|
|
public float yVelocity;
|
|
public float gravity= -9f;
|
|
public float movingSpeed = 1f;
|
|
public float jumpForce = 1f;
|
|
public float jumpInterval = 0.1f;
|
|
float jumpTimer =0;
|
|
public Transform rotateVisual;
|
|
public float rotateIntensity = 10f;
|
|
|
|
public bool queueJump = false;
|
|
|
|
void OnValidate()
|
|
{
|
|
if(rb == null){
|
|
rb = GetComponent<PredictedRigidbody2D>();
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
HandleRotation();
|
|
|
|
if(isLocalPlayer){
|
|
HandleKeyboardInput();
|
|
HandleMouseInput();
|
|
}
|
|
|
|
if(isServer || isLocalPlayer){
|
|
HandleJump();
|
|
HandleMovement();
|
|
}
|
|
}
|
|
|
|
#region input
|
|
void HandleKeyboardInput(){
|
|
if(Input.GetKeyDown(KeyCode.Space)){
|
|
OnJumpClicked();
|
|
}
|
|
}
|
|
|
|
void HandleMouseInput(){
|
|
if(Input.GetMouseButtonDown(0)){
|
|
OnJumpClicked();
|
|
}
|
|
}
|
|
|
|
public void OnJumpClicked(){
|
|
if(isServer){
|
|
queueJump = true;
|
|
}else{
|
|
CmdOnJumpClicked();
|
|
queueJump=true;
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
void CmdOnJumpClicked(){
|
|
queueJump = true;
|
|
}
|
|
#endregion
|
|
|
|
#region Movement
|
|
void HandleMovement(){
|
|
// yVelocity += gravity * Time.deltaTime;
|
|
// transform.position += new Vector3(0, yVelocity * Time.deltaTime, 0);
|
|
}
|
|
|
|
void HandleRotation(){
|
|
rotateVisual.rotation = Quaternion.Euler(0, 0, rb.predictedRigidbody.velocity.y * rotateIntensity);
|
|
}
|
|
|
|
void HandleJump(){
|
|
if(jumpTimer < jumpInterval){
|
|
jumpTimer += Time.deltaTime;
|
|
return;
|
|
}
|
|
if(queueJump){
|
|
if(jumpTimer >= jumpInterval){
|
|
jumpTimer = 0;
|
|
queueJump = false;
|
|
Jump();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Jump(){
|
|
// yVelocity = jumpForce;
|
|
Debug.Log("Jumped at " + NetworkTime.time);
|
|
rb.predictedRigidbody.velocity = new Vector2(rb.predictedRigidbody.velocity.x, jumpForce);
|
|
}
|
|
#endregion
|
|
}
|