Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityStandardAssets.CrossPlatformInput;
- public class Mouvement : MonoBehaviour
- {
- Rigidbody rb;
- private bool canMove = true;
- public bool rotate = false;
- private int lastRotation = 0;
- public float speed = 1f;
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- }
- //Buttons
- public void RotateButton(bool right)
- {
- if (!canMove)
- return;
- float temp = 90;
- if (!right)
- temp = -90;
- StartCoroutine(RotateMe(Vector3.up * temp, 0.5f, right));
- }
- IEnumerator RotateMe(Vector3 byAngles, float inTime, bool right)
- {
- canMove = false;
- var fromAngle = transform.rotation;
- var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
- for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
- {
- transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
- yield return null;
- }
- transform.eulerAngles = new Vector3(0, CheckRotationAngle(right), 0);
- canMove = true;
- }
- private float CheckRotationAngle(bool right)
- {
- int temp = 0;
- switch (lastRotation)
- {
- case 0:
- if (right)
- temp = 90;
- else
- temp = -90;
- break;
- case 90:
- if (right)
- temp = 180;
- else
- temp = 0;
- break;
- case 180:
- if (right)
- temp = -90;
- else
- temp = 90;
- break;
- case -90:
- if (right)
- temp = 0;
- else
- temp = -180;
- break;
- case -180:
- if (right)
- temp = -90;
- else
- temp = 90;
- break;
- }
- lastRotation = temp;
- return (float)temp;
- }
- private void FixedUpdate()
- {
- if (!GameInfo.CanMove)
- {
- rb.velocity = Vector3.zero;
- return;
- }
- float translation = CrossPlatformInputManager.GetAxis("Vertical") * speed;
- float straffe = CrossPlatformInputManager.GetAxis("Horizontal") * speed;
- translation *= Time.deltaTime;
- straffe *= Time.deltaTime;
- transform.Translate(straffe, 0, translation);
- if(straffe + translation == 0)
- {
- if(rb.velocity != Vector3.zero)
- rb.velocity = Vector3.zero;
- }
- }
- public bool GetCanMove()
- {
- return canMove;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement