Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with boardgame like movement

Discussion in 'Scripting' started by ESCPE, Aug 11, 2016.

  1. ESCPE

    ESCPE

    Joined:
    Jun 12, 2013
    Posts:
    40
    Hey!

    So i wanna make a mario party like game. And what im having problem with is the movement, i got waypoints around the map and when i roll the dice i want to go as many steps as the dice gave me.

    This is how far i've, and the problem is for example when im at waypoint 2 and i roll a 3 on the dice it goes to the 3 waypoint, but what it should go to is the fifth waypoint.

    Help please!

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Movement : MonoBehaviour {
    6.  
    7.  
    8.     private Dice dice;
    9.     public GameObject diceObj;
    10.     public float speed;
    11.     public int i = 0;
    12.     public Transform[] wayPoint;
    13.     public int targetWaypoint;
    14.  
    15.     bool asd = true;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         dice = diceObj.GetComponent<Dice>();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.        
    28.         Vector3 target = wayPoint[i].position;
    29.         float maxDist = speed * Time.deltaTime;
    30.        
    31.  
    32.         if(transform.position == target)
    33.         {
    34.             i++;
    35.         }
    36.  
    37.         if(i == wayPoint.Length)
    38.         {
    39.             i = 0;
    40.         }
    41.        
    42.  
    43.         if(true)
    44.         {  
    45.         transform.position = Vector3.MoveTowards(transform.position, target, maxDist);
    46.        
    47.        
    48.         }
    49.  
    50.  
    51.  
    52.         transform.LookAt(target);
    53.         Debug.Log(dice.steps);
    54.  
    55.      
    56.  
    57.        
    58.        
    59.         //dice.steps;
    60.    
    61.     }
    62. }
    63.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    You'll want to keep track of where each player is. So if a person is in index 2 position, then you have an int for that player (could just have a script on the player that tracks data for that player). Then if you roll a 3, you take that index, add 3 to it, then you get 5 back. Move to that position by looping through the array from 3-5 to give the look of moving across the board.
     
    LiterallyJeff likes this.
  3. ESCPE

    ESCPE

    Joined:
    Jun 12, 2013
    Posts:
    40
    I guess that's easier said than done in my case... appreciate the answer.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Not really. Say you have a script called player. This script has a variable called currentPosition. You roll the dice and get the value of currentPosition and then add the dice roll to it to get where they are suppose to move to. Then you'll loop through positions from currentPosition to endPosition so the piece moves along. You could start the movement and have triggers at each spot when the piece arrives it checks if the piece should keep moving or if it arrives at it's endPosition, the turn ends.