private void Update() { switch (state) { case State.Stay: // do Nothing!! break; case State.Walk: if (lastState != State.Walk) { OnStartMove.Invoke(); } var dir = (to.transform.position - this.transform.position).normalized; this.transform.Translate(dir * speed * Time.deltaTime); if (Vector3.Distance(to.transform.position, this.transform.position) < 0.01f) { state = State.Stay; tile = to; OnFinishMove.Invoke(); } break; } lastState = state; }
public void Update() { if (Input.GetAxis("Horizontal") != 0.0f || Input.GetAxis("Vertical") != 0.0f) { var dir = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0).normalized; float directionError = float.MaxValue; BehaviourTile to = null; foreach (var neighbor in character.tile.neighbors) { var dirNeighbor = Vector3.Normalize(neighbor.transform.position - character.tile.transform.position); var dirError = Vector3.Distance(dirNeighbor, dir.normalized); if (Vector3.Distance(dirNeighbor, dir.normalized) < directionError) { directionError = dirError; to = neighbor; } } if (to != null && directionError < 0.4f) { character.Move(to); } } }
public void Move(BehaviourTile to) { state = State.Walk; this.animator.SetBool("Move", true); this.to = to; }