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
0
Question by Awesomeness1075 · Nov 28, 2017 at 02:50 AM · networkingspawnclientcommandhost

Why will the raycast not work on the client?

I have it so that both the host and client can spawn cubes on the map by clicking on it at the beginning of the game. I can spawn cubes perfectly on the server but it spawns the cubes at (0,0,0) on the client. Here is the current script.

 [SerializeField] public Transform shootLocation;
 [SerializeField] public GameObject cannonBall;
 [SerializeField] public float shootingSpeed;
 [SerializeField] public static int blocks;
 [SerializeField] public GameObject cube;
 private Vector3 dropLocation;

 void Start () {
     blocks = 5;
 }
 
 void Update () {

     if (Input.GetMouseButtonDown(0))
     {
         if (blocks > 0)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.transform.name != "null" && hit.transform.name != "wall")
                 {
                     dropLocation = new Vector3(hit.point.x, hit.point.y + 1.5f, hit.point.z);
                     CmdSpawn();
                 }
             }
         }
         else
         {
             CmdFire();
         }
     }
 }

 [Command]
 void CmdSpawn()
 {
     blocks = blocks - 1;
     var block = Instantiate(cube, dropLocation, Quaternion.identity);
     NetworkServer.Spawn(block);
 }

 [Command]
 void CmdFire()
 {
     var shell = Instantiate(cannonBall, shootLocation.position, shootLocation.rotation);
     shell.GetComponent<Rigidbody>().velocity = shell.transform.forward * shootingSpeed;
     NetworkServer.Spawn(shell);
 }

}

It seems that when CmdSpawn is called, it is called for the host and not the client. Any help to get it spawned on the client will be greatly appreciated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ChrisKurhan · Dec 02, 2017 at 04:36 PM

If you change your CmdSpawn() to have CmdSpawn(Vector3 dropLocation) it should spawn the block at the expected location. Right now you assign dropLocation on the client in Update but this is not synced to the server in any way. I would expect your Raycast is working correctly but the Server does not know where to spawn your object.

As for CmdFire, you also probably will need to add the position and rotation as arguments unless you're using a NetworkTransform and the transform's position and rotation are already synchronized across Client/Server

Comment
Add comment · Show 1 · Share
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 Awesomeness1075 · Dec 09, 2017 at 07:16 PM 0
Share

It works! Thank you, I was about to give up and it was getting very frustrating. I will now try to continue on with the game and hopefully finish it soon.

avatar image
0

Answer by Awesomeness1075 · Nov 29, 2017 at 03:20 AM

I did some more testing and found that the cannon balls do spawn properly. Could it be a problem with the raycast or possibly its components?

Comment
Add comment · Share
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

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

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Networking bullet spawning issues between Host/Client 0 Answers

[Command] not sending to server? 1 Answer

Command not executing for all clients 0 Answers

Why is the command function not being called on the client? 0 Answers

Force local client RPC to fire immediately? 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