- Home /
Question by
danialwafiy · Apr 16 at 08:55 AM ·
multiplayerphotonpun
Unity photon player position not synching when colliding
I'm using Photon for multiplayer unity game. You can shoot balls by clicking space and move using wsad. When the ball hits the player it knocks the player back. I'm not adding any extra force when the ball hits the player. Its the default physics. The problem is, whenever player1 gets hit by a ball by player2, player1 position lags and isn't synching properly.
This is the demo: https://humbledev.itch.io/fight-me
Player prefab has a photon view, photon transform view classic and photon rigidbody view components to it.
PlayerController
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
anim = GetComponent<Animator>();
trail = GetComponent<TrailRenderer>();
view = GetComponent<PhotonView>();
}
private void Update()
{
if(view.IsMine){
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
IsJumping = Input.GetKeyDown(KeyCode.Space);
IsShooting = Input.GetMouseButtonDown(0);
IsGrounded = Physics.CheckSphere(groundCheck.position, groundRadius, groundMask);
if(IsJumping && IsGrounded){
rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(!IsGrounded){
trail.emitting = true;
}else{
trail.emitting = false;
}
if(IsShooting){
view.RPC("RPC_Shoot", RpcTarget.All);
}
}
}
private void FixedUpdate() {
movement = new Vector3(horizontalInput, 0, verticalInput).normalized;
if(movement != Vector3.zero){
anim.SetBool("IsRunning", true);
Quaternion targetRotation = Quaternion.LookRotation(movement);
targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
rb.MoveRotation(targetRotation);
}else{
anim.SetBool("IsRunning", false);
}
if(IsJumping && IsGrounded){
// rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(rb.velocity.y < 0){
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}else if(rb.velocity.y > 0 && !IsJumping){
rb.velocity += Vector3.up * Physics.gravity.y * (lowMultiplier - 1) * Time.deltaTime;
}
}
[PunRPC]
void RPC_Shoot(){
GameObject bullet = Instantiate(bulletPrefab, aim.transform.position, Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = aim.transform.forward * bulletSpeed;
}
private void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Bullet"){
view.RPC("RPC_Hit", RpcTarget.All, other.gameObject);
}
}
[PunRPC]
void RPC_Hit(GameObject item){
Destroy(item);
}
Comment