I Despratly Need Help.

I have a script for a board game I am making. each character has different abilities and such but in trying to get spaces that are supposed to automatically move the player forward or backward the script won’t even detect a simple collision. here is the script that is not working. I am just beginning my game dev career and still don’t quite understand all of the syntax so if something is wrong please tell me and explain it like you are talking to a four-year-old. Thank you. :smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireKid : MonoBehaviour
{
    public string characterName = "";
    public string moveSpaceTag = "";
    public int currentWaypoint;
    public int targetWaypoint;
    public int waypointsMoved = 0;
    public int diceRoll;
    public float moveSpeed = 5;
    public bool startedTurn = false;
    public TurnManager tm;
    public WaypointManager wm;
    public bool isOnMoveSpace = false;
    public GameObject lastMoveSpace;

    void Start()
    {
        tm = FindObjectOfType<TurnManager>();
        wm = FindObjectOfType<WaypointManager>();
     
        for (int i = 0; i < tm.activePlayers.Length; i++)
        {
            if (tm.activePlayers[i].name == characterName);
        }
    }


    void Update()
    {
        if (tm.activePlayers[tm.playerTurn].name == characterName)
        {
            if (Input.GetKeyDown(KeyCode.E) && !startedTurn)
            {
                startedTurn = true;
                RollDie();
            }
         
            if (startedTurn)
            {
                if (Vector2.Distance(this.gameObject.transform.position, wm.waypoints[currentWaypoint + waypointsMoved].transform.position) == 0)
                    waypointsMoved++;
                if (currentWaypoint <= targetWaypoint)
                {
                    this.gameObject.transform.position = Vector2.MoveTowards(this.gameObject.transform.position, wm.waypoints[currentWaypoint + waypointsMoved].transform.position, moveSpeed * Time.deltaTime);

                }
                if (Vector2.Distance(this.gameObject.transform.position, wm.waypoints[targetWaypoint].transform.position) == 0)
                {
                    startedTurn = false;
                    currentWaypoint = targetWaypoint;
                    //diceRoll = 0;
                    waypointsMoved = 0;
                 
                    if (isOnMoveSpace == true)
                    {
                        MovementSpace movementSpace = lastMoveSpace.GetComponent<MovementSpace>();

                        targetWaypoint = movementSpace.spacesToMove;

                        isOnMoveSpace = false;
                    }
                 
                    tm.NextTurn();
                }
            }
        }
    }

    public void RollDie()
    {
        diceRoll = Random.Range(1, 7);

        if (targetWaypoint < wm.waypoints.Length - diceRoll)
            targetWaypoint += diceRoll;
        else
            targetWaypoint = wm.waypoints.Length - 1;
    }

    public void OnTriggerStay2D(Collider2D collision)
    {
        Debug.Log("Collision.");
    }
}

There are very specific rules for collision detection or else it won’t work.

Review the docs for all the requirements, I won’t retype them here.

Also, in your case above you are actually implementing a 2D Trigger callback, not a 2D Collision callback.

Also, I see you moving things by directly manipulating the transform. If those things are part of the physics and collisions you desire, you are likely going to have additional problems. Here’s why:

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

https://discussions.unity.com/t/866410/5

https://discussions.unity.com/t/878046/8

2 Likes

Thanks, I did not know that directly changing the transform canceled collisions. That helped a lot. some more issues are happening but I think I can figure them out.

1 Like