Hello, my question today deals with movement. So my games movement is like a chess board, and i only want my player to go so many tiles before he has to stop. So for example i want my player to move 3 tile away. I have created a OnTriggerEnter for the tiles and a action variable for the player. the trigger is suppose to take away on point from the action variable and when the action varaible equals 0 the players movement equal 0 as well. but thats not whats happening here are my 2 codes can anyone help me on whats going on.
Player script:
using UnityEngine;
using System.Collections;
public class Movement2 : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance;
public int maxAction = 3;
public int curAction = 3;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
float maxDistance = 0.1f;
}
// Update is called once per frame
void Update () {
// get the vector me->target - it will be very useful
Vector3 dir = target.position - myTransform.position;
// keep it horizontal to avoid character tilting if the heights differ:
dir.y = 0;
//Look at target using dir
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
if(dir.magnitude > maxDistance) { // dir.magnitude is the distance
//Move towards target using the character controller
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
AdjustCurrentAction(0);
if (curAction < 0)
moveSpeed = 0;
}
public void AdjustCurrentAction(int adj) {
curAction += adj;
if(curAction < 0)
curAction = 0;
if(curAction > maxAction)
curAction = maxAction;
if(maxAction < 1)
maxAction = 1;
}
}
Tile scripts (the OnTriggerEnter one)
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
void OnTriggerEnter(Collider other){
Movement2 m = (Movement2)GetComponent("Movement2");
m.AdjustCurrentAction(-1);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
thanks in advance!