How to make a Player move to an object using a dice roll
I am trying to make it so when you roll a dice and it lands a 2, you can select 2 spaces in any direction to move to.
Here is a code im using at the moment so that when you roll you move x spaces forward. I need it so you can select where you want to move, the amount of spaces you roll
public var dieName = "SixSidedDie";
public var allowedDistance = 0.5;
private var lastDieValue = 0;
private var target : Transform;
public var damping = 3.0;
private var dieValueComponent;
public var currentLocation = 1;
public var targetLocation = 1;
function Start() {
// cache the dieValueComponent
dieGameObject = GameObject.Find(dieName);
dieValueComponent = dieGameObject.GetComponent("DieValue");
// look for the starting position and move there
tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
transform.position = tileGameObject.transform.position;
transform.rotation = tileGameObject.transform.rotation;
}
function Update ()
{
if (dieValueComponent.playStarted) {
// get the new transform if necessary
// look for a non zero number which means the die has a value
// block any value that has already been calculated, hence checking last value
if (lastDieValue != dieValueComponent.currentValue && dieValueComponent.currentValue != 0 && dieValueComponent.turnBegun) {
targetLocation += dieValueComponent.currentValue;
currentLocation += 1;
if (currentLocation > 16) currentLocation = 16;
if (targetLocation > 16) targetLocaiton = 16;
tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
target = tileGameObject.transform;
dieValueComponent.turnBegun = false;
}
if (lastDieValue != 0) {
dist = Vector3.Distance(transform.position, target.position);
if (dist > allowedDistance) {
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * damping);
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, Time.deltaTime * damping);
}
else if (currentLocation != targetLocation) {
// reset the target
currentLocation += 1;
if (currentLocation > 16) currentLocation = 16;
tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
target = tileGameObject.transform;
}
}
lastDieValue = dieValueComponent.currentValue;
}
}
@$$anonymous$$ Did you find an answer to this yet of have you completed your project? If so can you post a link to your game & or tutorial? I'm just getting started on a board game I written years ago & I'm not venturing to video games.
Thanks a lot,
Spaz
Your answer
Follow this Question
Related Questions
How to make the 2D board game piece move only when the player answers the question correctly? 0 Answers
Rolling a 3D Dice: Detect which number faces up? 4 Answers
Collider disabling by itself... 2 Answers
How can I change my board game into multi player? 1 Answer
How to create an if statement for each element within a list C# 2 Answers