- Home /
Question by
FrozenDevs · Mar 25, 2021 at 08:12 PM ·
movementmultiplayerphotonjumping
How can I add jumping to my multiplayer photon script?
public PhotonView photonView;
public Rigidbody2D rb;
public Animator anim;
public GameObject PlayerCamera;
public SpriteRenderer sr;
public Text PlayerNameText;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public bool isGrounded = false;
public float MoveSpeed;
public float JumpForce;
private int extraJumps;
public int extraJumpValue;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
}
private void Awake()
{
if(photonView.isMine)
{
PlayerCamera.SetActive(true);
}
else
{
PlayerNameText.text = photonView.owner.name;
PlayerNameText.color = Color.red;
}
}
// Update is called once per frame
private void Update()
{
if(photonView.isMine)
{
CheckInput();
}
}
private void CheckInput()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position * MoveSpeed, rb.velocity.y);
if(isGrounded == true)
{
extraJumps = extraJumpValue;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps < 0)
{
rb.velocity = Vector2.up * JumpForce;
extraJumps--;
} else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * JumpForce;
}
var move = new Vector3(Input.GetAxisRaw("Horizontal"), 0);
transform.position += move * MoveSpeed * Time.deltaTime;
if(Input.GetKeyDown(KeyCode.A))
{
photonView.RPC("FlipTrue", PhotonTargets.AllBuffered);
}
if (Input.GetKeyDown(KeyCode.D))
{
photonView.RPC("FlipFalse", PhotonTargets.AllBuffered);
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
anim.SetBool("isRunning", true);
} else
{
anim.SetBool("isRunning", false);
}
}
[PunRPC]
private void FlipTrue()
{
sr.flipX = true;
}
[PunRPC]
private void FlipFalse()
{
sr.flipX = false;
}
Comment
Your answer
Follow this Question
Related Questions
Cant Jump ?c# please 2 Answers
Photon Player Controls Other Player 0 Answers
Players are lagging in Photon multiplayer 0 Answers
Jumping while moving 2 Answers
Player Smoothness across network (PUN) 0 Answers