Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Emielionen · May 16, 2016 at 01:46 PM · unity 5multiplayerprojectileshootertop-down

Shooting a projectile with Unet

Goodevening everyone!

I am stuck with a problem with Unet and projectiles.

Scenario:

The Player has a "Starting weapon" assigned to it, as in the following script which is assigned to the Player:

 public class GunController : NetworkBehaviour
 {
     public Transform weaponHold;
     public Gun startingGun;
     Gun equippedGun;
 
     void Start()
     {
         if (startingGun != null)
         {
             EquipGun(startingGun);
         }
     }
 
     public void EquipGun(Gun gunToEquip)
     {
         if (equippedGun != null)
         {
             Destroy(equippedGun.gameObject);
         }
         equippedGun = Instantiate(gunToEquip, weaponHold.position, weaponHold.rotation) as Gun;
         equippedGun.transform.parent = weaponHold;
     }
 
     public void Shoot()
     {
         if(equippedGun != null && isLocalPlayer)
         {
             equippedGun.CmdShoot(equippedGun.muzzle.position, equippedGun.muzzle.rotation);
         }
     }

The gun is instantiated when the player spawns, as a child of the Player's weaponhold child. This class will also execute the command for shooting the gun.

The Command for shooting is in the Gun class:

 public class Gun : NetworkBehaviour {
 
     public Transform muzzle;
     public Projectile projectile;
     public float msBetweenShots = 100;
     public float muzzleVelocity = 35;
 
     float nextShotTime;
 
     [Command]
     public void CmdShoot(Vector3 muzzlePos, Quaternion muzzleRot)
     {
         if (Time.time > nextShotTime)
         {
             nextShotTime = Time.time + msBetweenShots / 1000;
             Projectile newProjectile = Instantiate(projectile, muzzlePos, muzzleRot) as Projectile;
             newProjectile.SetSpeed(muzzleVelocity);
             NetworkServer.SpawnWithClientAuthority(newProjectile.gameObject, transform.parent.parent.gameObject);
         }
     }
 }

The Gun prefab also has a networkIdentity, with its local playerAuthority checked.

The input for the shooting is handeled with the Player class:

 [RequireComponent(typeof(PlayerController))]
 [RequireComponent(typeof(GunController))]
 public class Player : NetworkBehaviour
 {
     public float moveSpeed = 5;
     PlayerController playerController;
     Camera viewCamera;
     GunController gunController;
 
     void Start()
     {
         playerController = GetComponent<PlayerController>();
         gunController = GetComponent<GunController>();
         viewCamera = Camera.main;
     }
 
     void Update()
     {
         if (isLocalPlayer)
         {
             // Movement Input
             Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
             Vector3 moveVelocity = moveInput.normalized * moveSpeed;
             playerController.Move(moveVelocity);
 
             // Rotation Input
             Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
             Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
             float rayDistance;
 
             if (groundPlane.Raycast(ray, out rayDistance))
             {
                 Vector3 point = ray.GetPoint(rayDistance);
                 playerController.LookAt(point);
             }
 
             // Shooting Input
             if (Input.GetMouseButton(0))
             {
                 gunController.Shoot();
             }
         }
     }
 }

So if everything should work as I indended, the result would be:

There will be a projectile gameObject spawned each time the command is called. but instead there is this warning: "Trying to send command for object without authority."

Does anyone know how this would work without changing the hierarchy of my code?

Thanks in advance!

Comment
Add comment · Show 1
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 meat5000 ♦ · May 16, 2016 at 08:04 AM 0
Share

Here's a little snippet I use that works for me. I have things like bullets spawn with Client Authority as I want to spread the load of the processing between Clients rather than leaving the Server burdened with all the work. There is a trade off to be had between shifting the processing work and increasing the Network load.

This is the part of a player script which handles Firing.

 @Client
 public function ShootButtonPress() :void
 {
     if(isLocalPlayer) CmdSpawnOnNetwork();
 }
 
 @Command
 function CmdSpawnOnNetwork()
 {    
     var myPattern = myGun.bulletPattern;
     var projectileObject = Instantiate(myGun.bulletType, Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation * Quaternion.Euler(0, 180, 0));
     NetworkServer.SpawnWithClientAuthority(projectileObject, netID.connectionToClient);
     if(isServer) RpcSwerve(projectileObject);
 }
 
 @ClientRpc
 function RpcSwerve(projObject:GameObject)
 {
     projObject.AddComponent(SwerveShot); 
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need urgent help to create projectile shooting script with raycasting 1 Answer

Raycasting over Photon Unity Networking 1 Answer

How to disable the runtime GUI on the Network Manager Hud when you connect to a server?,How do you disable the runtime GUI on the Network Manager Hud when connected to a server 0 Answers

Firing projectiles on the XZ Plane 2 Answers

How to receive a callback that message has been received in google play games services for unity? 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