Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine; using System.Collections;
- public class TileMovement : MonoBehaviour {
- public string LastMoveDirection;
- private float speed = 2.0f;
- public float dash_speed = 0f;
- public string RotateCharacter;
- private bool Rotated = false;
- [SerializeField] private Vector3 pos;
- private Transform transform;
- private CharacterController characterController;
- private Vector3 lastRoundedPosition;
- void Awake()
- {
- //Here we set the Values from the current position
- pos = base.transform.position;
- transform = base.transform;
- characterController = this.GetComponent<CharacterController>();
- }
- //void OnControllerColliderHit(ControllerColliderHit hit)
- //{
- // Debug.Log("Hit somthing " + hit.transform.tag);
- // if (hit.transform.tag == "FloorTile")
- // {
- // hit.gameObject.GetComponent<TileRotation>().FlipTile();
- // }
- //}
- void Update()
- {
- Movement();
- // transform.position = pos;
- }
- private void Movement()
- {
- //If we press any Key we will add a direction to the position ...
- //using Vector3.'anydirection' will add 1 to that direction
- //But we Check if we are at the new Position, before we can add some more
- //it will prevent to move before you are at your next 'tile'
- if (Input.GetButtonDown("Horizontal"))
- {
- if (Input.GetAxisRaw("Horizontal") == 1) //&& rigidbody.position == pos)
- {
- // pos += Vector3.right;
- transform.rotation = Quaternion.LookRotation(Vector3.right);
- if(Input.GetButton("LeftShift"))
- {
- characterController.Move(Vector3.right*dash_speed);
- }
- else
- {
- characterController.Move(Vector3.right);
- }
- LastMoveDirection = "right";
- }
- else if (Input.GetAxisRaw("Horizontal") == -1)// && rigidbody.position == pos)
- {
- // pos += Vector3.left;
- transform.rotation = Quaternion.LookRotation(Vector3.left);
- if (Input.GetButton("LeftShift"))
- {
- characterController.Move(Vector3.left*dash_speed);
- }
- else
- {
- characterController.Move(Vector3.left);
- }
- LastMoveDirection = "left";
- }
- }
- if (Input.GetButtonDown("Vertical"))
- {
- if (Input.GetAxisRaw("Vertical") == 1)//&& rigidbody.position == pos)
- {
- // pos += Vector3.forward;
- transform.rotation = Quaternion.LookRotation(Vector3.forward);
- if (Input.GetButton("LeftShift"))
- {
- characterController.Move(Vector3.forward*dash_speed);
- }
- else
- {
- characterController.Move(Vector3.forward);
- }
- LastMoveDirection = "forward";
- }
- else if (Input.GetAxisRaw("Vertical") == -1)// && rigidbody.position == pos)
- {
- // pos += Vector3.back;
- transform.rotation = Quaternion.LookRotation(Vector3.back);
- if (Input.GetButton("LeftShift"))
- {
- characterController.Move(Vector3.back*dash_speed);
- }
- else
- {
- characterController.Move(Vector3.back);
- }
- LastMoveDirection = "back";
- }
- }
- // characterController.Move(new Vector3(Mathf.FloorToInt(characterController.transform.position.x), 0, Mathf.FloorToInt(characterController.transform.position.z)));
- characterController.transform.position = new Vector3(
- Mathf.RoundToInt(characterController.transform.position.x),
- Mathf.RoundToInt(1),
- Mathf.RoundToInt(characterController.transform.position.z)
- );
- }
- //Here you will move Towards the new position ...
- //transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement