- Home /
Photon - Add force to other player
That's my Gun script for my 2d game, i'm trying to add force to the player hit, but this does not work. if I hit a sprite that has not been instantiated works, it will not work.
public class Shotgun : Photon.MonoBehaviour {
public LayerMask whatToHit;
Transform firePoint;
// Use this for initialization
void Start () {
firePoint = transform.FindChild("FirePoint");
}
// Update is called once per frame
void Update () {
gunMovment();
//Controllo se ha sparato
if (Input.GetMouseButtonDown(0)) {
//fire_render.renderer.enabled = true;
shoot();
}
}
//Regolo arma in base alla posizione del mouse
void gunMovment()
{
Vector3 mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23f; //la distanza tra la camera che lo segue e l'oggetto
Vector3 object_pos = Camera.main.WorldToScreenPoint (transform.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
//sparoooo
void shoot(){
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition - firePointPosition , 3, whatToHit);
//Debug.DrawLine (firePointPosition, mousePosition - firePointPosition, Color.cyan);
if (hit.collider != null) {
//Controllo se viene colpito un player
if(hit.collider.tag == "Player")
{
hit.rigidbody.AddForceAtPosition(100 * mousePosition - firePointPosition, hit.point);
}
//Debug.DrawLine(firePointPosition, hit.point, Color.red);
//Debug.Log ("We hit " + hit.collider.tag + " and did " + Damage + " damage.");
}
}
Look in to the usage of RPCs (remote procedure calls) with Photon. This will allow you to send functions over the network to keep things updated between players. You can send a Vector3 with the force to add to the player via RPC and let the RPC handle adding it on both computers.
Hopefully that helps.
Useful links:
http://doc.exitgames.com/en/pun/current/getting-started/pun-intro
http://doc.exitgames.com/en/pun/current/tutorials/tutorial-marco-polo
ok for RPC but how can i do, to execute the command only player to hit? I See Tatgets.all but it is not what I want.
Targets.all IS what you want. You want all players to see the increase in force. In the RPC, you want to make sure you only add the force to the player that owns the object. so, in the RPC you would use something along the lines of:
if(photonView.is$$anonymous$$ine){
AddForce(forceToAdd);
}
You can also pass other things, like PhotonNetwork.Player.ID as an int and check against that.
i edit my code, but it still does not work. Now the force is applied only to the player that spara.E you only see on his client
//sparoooo
void shoot(){
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition - firePointPosition , 3, whatToHit);
//Debug.DrawLine (firePointPosition, mousePosition - firePointPosition, Color.cyan);
if (hit.collider != null) {
//Controllo se viene colpito un player
if(hit.collider.tag == "Player")
{
print ("ho colpito player");
networkView.RPC("sonoStatoColpito", PhotonTargets.Others);
}
//Debug.DrawLine(firePointPosition, hit.point, Color.red);
//Debug.Log ("We hit " + hit.collider.tag + " and did " + Damage + " damage.");
}
}
[RPC]
public void sonoStatoColpito()
{
print ("sono qui");
if(!networkView.is$$anonymous$$ine) {
this.rigidbody2D.AddForce(Vector2.up * 300);
}
}
I have to serialize something?
Your answer
![](https://koobas.hobune.stream/wayback/20220613183601im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding Force to a Rigidbody through OnTriggerEnter 1 Answer
collider not working when apply "addForce" high enough 2 Answers
Internal collisions 1 Answer
Collision doesnt work for big distances 1 Answer
RigidBody falls through the ground 0 Answers