1
\$\begingroup\$

when the start game in the client [localserver], the bullet can't forward in the space just fixed in the Gun, here image about what I say:https://i.stack.imgur.com/AjMPG.jpg "image"

network manager https://i.stack.imgur.com/ksHBg.jpg "image"

player https://i.stack.imgur.com/7kdr2.jpg "image"

bullet https://i.stack.imgur.com/s9Nz5.jpg "image"

and this my script

    void Update()
   {
      if(!isLocalPlayer){
          return;
      }
      if (Input.GetKey(KeyCode.Mouse0))
      {
         CmdBulletFire();
      }
   }
  [Command]
  void CmdBulletFire()
  {
    // create the bullet prefab
    GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);

    //add velocity to bullet
    bullet.GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.forward * 50000f));
    //Spawn the bullet
    NetworkServer.Spawn(bullet);
    //Destroy the bullet
    Destroy(bullet, 5f);
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I am sharing my code which works fine use this on hope so it will work

 public   void OnButtonDown()
{

    GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
    Rigidbody rb = bullet.GetComponent<Rigidbody>();
    FireClick.Play ();
    bullet.transform.rotation = Camera.main.transform.rotation;
    bullet.transform.position = Camera.main.transform.position;
    rb.AddForce(Camera.main.transform.forward * 500f);
    Destroy(bullet, 3);




}

use this

 rb.AddForce(Camera.main.transform.forward * 500f);
\$\endgroup\$
1
  • \$\begingroup\$ thank u , but no problem with non network, the problem in the networking the bullet can't move \$\endgroup\$
    – Anas Te
    May 9, 2018 at 17:11
1
\$\begingroup\$

'Vector3.forward' is shorthand for writing Vector3(0, 0, 1).

What you're looking for is 'transform.forward'. This is the direction your transform is facing.

 bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 50000f, ForceMode.Impulse);
\$\endgroup\$
3
  • \$\begingroup\$ no problem with non network , the problem in the networking the bullet can't move \$\endgroup\$
    – Anas Te
    May 9, 2018 at 17:11
  • \$\begingroup\$ Did you try setting 'TransformSyncMode' = SyncRigidbody on the NetworkTransform component of the bullet prefab? \$\endgroup\$
    – akkwake
    May 9, 2018 at 21:44
  • \$\begingroup\$ yes but still the issue, but when removing the Network Manager and set try again the code is run without issue! did you know what's happened about that ? \$\endgroup\$
    – Anas Te
    May 9, 2018 at 23:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .