Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Netcode Objects dont inneract with each other

Discussion in 'Netcode for GameObjects' started by Gogunio, Feb 5, 2023.

  1. Gogunio

    Gogunio

    Joined:
    Mar 14, 2022
    Posts:
    10
    Hi
    I have a player that uses Client Network transform and my custom movemnet script
    Everything works fine but the players cant inneract with each other

    here is my movement script:


    using System.Collections;
    using UnityEngine;
    using Unity.Netcode;

    public class Movement : NetworkBehaviour
    {
    /// <Movement>
    public GameObject leftLeg;
    public GameObject rightLeg;
    Rigidbody2D leftLegRB;
    Rigidbody2D rightLegRB;
    public Rigidbody2D rb;
    /// </Movement>

    public Animator anim;

    [SerializeField] float speed = 1.5f;
    [SerializeField] float stepWait = .5f;
    [SerializeField] float jumpForce = 10;

    /// <Jumping>
    private bool isOnGround;
    public float positionRadius;
    public LayerMask ground;
    public Transform playerPos;
    /// </Jumping>

    // Start is called before the first frame update
    void Start()
    {
    leftLegRB = leftLeg.GetComponent<Rigidbody2D>();
    rightLegRB = rightLeg.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    if (!IsOwner) return;

    if (Input.GetAxisRaw("Horizontal") != 0)
    {
    if (Input.GetAxisRaw("Horizontal") > 0)
    {
    anim.Play("PlayerWalkRight");
    StartCoroutine(MoveRight(stepWait));
    }
    else
    {
    anim.Play("PlayerWalkLeft");
    StartCoroutine(MoveLeft(stepWait));
    }
    }
    else
    {
    anim.Play("PlayerIdle");
    }

    isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground);
    if (isOnGround == true && (Input.GetKeyDown(KeyCode.W)))
    {
    rb.AddForce(Vector2.up * (jumpForce * 100));
    }


    }

    IEnumerator MoveRight(float seconds)
    {

    leftLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime);
    yield return new WaitForSeconds(seconds);
    rightLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime);

    }
    IEnumerator MoveLeft(float seconds)
    {
    rightLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime);
    yield return new WaitForSeconds(seconds);
    leftLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime);
    }
    }


    What can i change to make my players inneract with each other?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @Gogunio , what do you mean by this? Are you talking about collisions? if so, you should check the settings of your colliders/rigidbody (this is unrelated to networking functionality)
     
  3. Gogunio

    Gogunio

    Joined:
    Mar 14, 2022
    Posts:
    10
    I mean that the players cant push each other
    If this is caused by the movement script then i would like to know what can i change in this script
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Do your objects have the collider component?
     
  5. Gogunio

    Gogunio

    Joined:
    Mar 14, 2022
    Posts:
    10
    Yes, they have a Capsule collider 2D
     
  6. Gogunio

    Gogunio

    Joined:
    Mar 14, 2022
    Posts:
    10
    The goal is to make objects push each other but when the player 1 tries to push player 2, player 2 just doesn't move