Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ninhow · Jan 15 at 08:08 PM · unity 5networkingspawning

Why does my server show different position for my client?

Hi, I am trying to spawn my player at a random location on the plane. I was following the tutorial of Dilmer from Youtube (https://www.youtube.com/watch?v=GOtE96OKyVA&list=PLQMQNmwN3FvyyeI1-bDcBPmZiSaDMbFTi∈dex=3).


So here is the problem: When the client connects it sets a random position on the plane to be spawned on. The problem is that the player always spawns on the position that the prefab has which is (0,0,0) and when another client connects it bugs out because it is on the same position as another client.

Here is the player control class:

using Unity.Netcode; using UnityEngine;

[RequireComponent(typeof(NetworkObject))] public class PlayerControl : NetworkBehaviour { public enum PlayerState { Idle, Walk, ReverseWalk, }

 [SerializeField]
 private float speed = 3.5f;

 [SerializeField]
 private float rotationSpeed = 3.5f;

 [SerializeField]
 private Vector2 defaultInitialPositionOnPlane = new Vector2(-15, 15);

 [SerializeField]
 private NetworkVariable<Vector3> networkPositionDirection = new NetworkVariable<Vector3>();

 [SerializeField]
 private NetworkVariable<Vector3> networkRotationDirection = new NetworkVariable<Vector3>();

 [SerializeField]
 private NetworkVariable<PlayerState> networkPlayerState = new NetworkVariable<PlayerState>();

 private CharacterController characterController;

 // client caches positions
 private Vector3 oldInputPosition = Vector3.zero;
 private Vector3 oldInputRotation = Vector3.zero;

 private Animator animator;

 private void Awake()
 {
     characterController = GetComponent<CharacterController>();
     animator = GetComponent<Animator>();
 }

 

 void Start()
 {
     if (IsClient && IsOwner)
     {
         transform.position = new Vector3(Random.Range(defaultInitialPositionOnPlane.x, defaultInitialPositionOnPlane.y), 0,
                Random.Range(defaultInitialPositionOnPlane.x, defaultInitialPositionOnPlane.y));
     }

 }

 void Update()
 {
     if (IsClient && IsOwner)
     {
         ClientInput();
     }

     ClientMoveAndRotate();
     ClientVisuals();
 }

 private void ClientMoveAndRotate()
 {
     if (networkPositionDirection.Value != Vector3.zero)
     {
         characterController.SimpleMove(networkPositionDirection.Value);
     }
     if (networkRotationDirection.Value != Vector3.zero)
     {
         transform.Rotate(networkRotationDirection.Value, Space.World);
     }
 }

 private void ClientVisuals()
 {
     if (networkPlayerState.Value == PlayerState.Walk)
     {
         animator.SetFloat("Walk", 1);
     }
     else if (networkPlayerState.Value == PlayerState.ReverseWalk)
     {
         animator.SetFloat("Walk", -1);
     }
     else
     {
         animator.SetFloat("Walk", 0);
     }
 }

 private void ClientInput()
 {
     // y axis client rotation
     Vector3 inputRotation = new Vector3(0, Input.GetAxis("Horizontal"), 0);

     // forward & backward direction
     Vector3 direction = transform.TransformDirection(Vector3.forward);
     float forwardInput = Input.GetAxis("Vertical");
     if (Input.GetKey(KeyCode.LeftShift) && forwardInput > 0) forwardInput = 2;

     Vector3 inputPosition = direction * forwardInput;


     if (oldInputPosition != inputPosition ||
         oldInputRotation != inputRotation)
     {
         oldInputPosition = inputPosition;
         UpdateClientPositionAndRotationServerRpc(inputPosition * speed, inputRotation * rotationSpeed);
     }

     if (forwardInput > 0 && forwardInput <= 1)
     {
         UpdatePlayerStateServerRpc(PlayerState.Walk);
     }
     else if (forwardInput < 0)
     {
         UpdatePlayerStateServerRpc(PlayerState.ReverseWalk);
     }
     else
     {
         UpdatePlayerStateServerRpc(PlayerState.Idle);
     }
 }

 [ServerRpc]
 public void UpdateClientPositionAndRotationServerRpc(Vector3 newPosition, Vector3 newRotation)
 {
     networkPositionDirection.Value = newPosition;
     networkRotationDirection.Value = newRotation;
 }

 [ServerRpc]
 public void UpdatePlayerStateServerRpc(PlayerState state)
 {
     networkPlayerState.Value = state;
 }

}

How can I spawn a player randomly on the new Unity MLAPI? Shouldn't the player get the random position set by the client? The prefab has NetworkObject, NetworkTransform scripts.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ninhow · Jan 18 at 08:43 PM

Solved buy doing that on connection approval.

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

280 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 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 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

Question about Unet and spawning 0 Answers

New Unity 5.1 Networking :- Spawning is not synchronized!!!! 2 Answers

Change player object - UNET 1 Answer

Transform.parent not set on clients UNET 0 Answers

Networking: How to spawn dynamically created objects 2 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