Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How do set the initial position of a rigidbody with a network transform.

Discussion in 'Multiplayer' started by spirail, Feb 17, 2023.

  1. spirail

    spirail

    Joined:
    Oct 16, 2017
    Posts:
    1
    I have a button that spawns in a gameobject with a Network Rigidbody and a Network transform.

    The game object prefab is at (0,0,0). But If I want to set an initial position to (-3,0,0), what I see on the client is an object spawning at (0,0,0) then quickly sliding into place at (-3,0,0).

    Even if I look carefully on the server instance, I see a brief flash where the object is at (0,0,0).

    What am I missing here?

    Code (CSharp):
    1.  
    2.  
    3. public class RobotControl : NetworkBehaviour
    4. {
    5.     new private Rigidbody rigidbody;
    6.  
    7.     public Vector3 defaultPosition = new Vector3(-3, 0, 0);
    8.  
    9.     public override void OnNetworkSpawn()
    10.     {
    11.         if (NetworkManager.Singleton.IsServer){
    12.             rigidbody = GetComponent<Rigidbody>();
    13.             rigidbody.position = defaultPosition;
    14.         }
    15.     }
    16. }
    17.  
    I'm running netcode 1.2.0
     
  2. speedhawk21

    speedhawk21

    Joined:
    Apr 30, 2014
    Posts:
    5
    This is an older post but for anyone else that comes across, I found calling rigidBody.MovePosition(customPosition) will work. (edit to update move position)
     
    Last edited: Mar 7, 2024
  3. cafeho

    cafeho

    Joined:
    Jun 4, 2022
    Posts:
    2
    thx. This helped me out in setting my own spawn points. To clarify though. rigidbody does not have SetPosition but MovePosition.
     
    speedhawk21 likes this.
  4. speedhawk21

    speedhawk21

    Joined:
    Apr 30, 2014
    Posts:
    5
    You're right, I was typing too fast! MovePosition is what worked for me.