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

Question I Despratly Need Help.

Discussion in 'Scripting' started by Pixel8tedPotato, May 3, 2024.

  1. Pixel8tedPotato

    Pixel8tedPotato

    Joined:
    Oct 5, 2023
    Posts:
    2
    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. :D

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FireKid : MonoBehaviour
    6. {
    7.     public string characterName = "";
    8.     public string moveSpaceTag = "";
    9.     public int currentWaypoint;
    10.     public int targetWaypoint;
    11.     public int waypointsMoved = 0;
    12.     public int diceRoll;
    13.     public float moveSpeed = 5;
    14.     public bool startedTurn = false;
    15.     public TurnManager tm;
    16.     public WaypointManager wm;
    17.     public bool isOnMoveSpace = false;
    18.     public GameObject lastMoveSpace;
    19.  
    20.     void Start()
    21.     {
    22.         tm = FindObjectOfType<TurnManager>();
    23.         wm = FindObjectOfType<WaypointManager>();
    24.      
    25.         for (int i = 0; i < tm.activePlayers.Length; i++)
    26.         {
    27.             if (tm.activePlayers[i].name == characterName);
    28.         }
    29.     }
    30.  
    31.  
    32.     void Update()
    33.     {
    34.         if (tm.activePlayers[tm.playerTurn].name == characterName)
    35.         {
    36.             if (Input.GetKeyDown(KeyCode.E) && !startedTurn)
    37.             {
    38.                 startedTurn = true;
    39.                 RollDie();
    40.             }
    41.          
    42.             if (startedTurn)
    43.             {
    44.                 if (Vector2.Distance(this.gameObject.transform.position, wm.waypoints[currentWaypoint + waypointsMoved].transform.position) == 0)
    45.                     waypointsMoved++;
    46.                 if (currentWaypoint <= targetWaypoint)
    47.                 {
    48.                     this.gameObject.transform.position = Vector2.MoveTowards(this.gameObject.transform.position, wm.waypoints[currentWaypoint + waypointsMoved].transform.position, moveSpeed * Time.deltaTime);
    49.  
    50.                 }
    51.                 if (Vector2.Distance(this.gameObject.transform.position, wm.waypoints[targetWaypoint].transform.position) == 0)
    52.                 {
    53.                     startedTurn = false;
    54.                     currentWaypoint = targetWaypoint;
    55.                     //diceRoll = 0;
    56.                     waypointsMoved = 0;
    57.                  
    58.                     if (isOnMoveSpace == true)
    59.                     {
    60.                         MovementSpace movementSpace = lastMoveSpace.GetComponent<MovementSpace>();
    61.  
    62.                         targetWaypoint = movementSpace.spacesToMove;
    63.  
    64.                         isOnMoveSpace = false;
    65.                     }
    66.                  
    67.                     tm.NextTurn();
    68.                 }
    69.             }
    70.         }
    71.     }
    72.  
    73.     public void RollDie()
    74.     {
    75.         diceRoll = Random.Range(1, 7);
    76.  
    77.         if (targetWaypoint < wm.waypoints.Length - diceRoll)
    78.             targetWaypoint += diceRoll;
    79.         else
    80.             targetWaypoint = wm.waypoints.Length - 1;
    81.     }
    82.  
    83.     public void OnTriggerStay2D(Collider2D collision)
    84.     {
    85.         Debug.Log("Collision.");
    86.     }
    87. }
     
    Last edited: May 7, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,053
    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://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    Sluggy and Ryiah like this.
  3. Pixel8tedPotato

    Pixel8tedPotato

    Joined:
    Oct 5, 2023
    Posts:
    2
    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.
     
    Sluggy likes this.