Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by casteponters · Feb 17, 2015 at 05:44 PM · colliderphotonaddforce

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.");
         }
         
     }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fariel · Feb 18, 2015 at 08:57 AM 0
Share

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

avatar image casteponters · Feb 18, 2015 at 09:14 AM 0
Share

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.

avatar image Fariel · Feb 18, 2015 at 09:17 AM 0
Share

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.

avatar image casteponters · Feb 18, 2015 at 02:53 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges