Je travaille actuellement sur un projet où j'ai besoin d'une rotation d'un cube (monde) de 90 degrés lorsque j'appuie sur une flèche.(voir la vidéo ci-dessous à la 5ieme secondes)
https://vimeo.com/302458211
Mes recherches m'ont amené à ce tutoriel, qui m'a permis d'avoir une première ébauche de code. Mais à présent je n'arrive pas à changer les sens et les directions.
J'ai multiplié les "rotateObject()" que j'ai mis en public pour pouvoir y accéder depuis des boutons sur un canvas que cela n'a pas du tout fonctionner.
Ça n'a eu pour effet que tourner plus ou moins vite...
Merci d'avance à ceux qui m'aideront va sortir de ce problème dans lequel je tourne en rond !
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
public class ObjectScript : MonoBehaviour
{
public int rotationDirection = -1; // -1 for clockwise
// 1 for anti-clockwise
public int rotationStep = 10; // should be less than 90
// All the objects with which collision will be checked
public GameObject[] objectsArray;
private Vector3 currentRotation, targetRotation;
private Vector3 positionDiff, prevPosition, changedPosition, initialPosition;
private bool isRotating = false, swipping = false, moving = false, isCurrentObject = false;
private float distanceForRotation = 0.1f;
void Start()
{
initialPosition = transform.position;
}
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0) && !isRotating)
{
positionDiff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
positionDiff.z = 0;
prevPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
swipping = isRotating = false;
isCurrentObject = moving = true;
}
}
void Update()
{
if (Input.GetMouseButtonUp(0) && !isRotating && isCurrentObject)
{
if (!swipping) {
rotateObject();
rotateObject2();
rotateObject3();
rotateObject4();
}
else
placeObjectAtProperPosition();
moving = swipping = isRotating = isCurrentObject = false;
}
else if (moving && GetTouchState())
{
if (!swipping && (Vector3.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition), prevPosition) > distanceForRotation))
swipping = true;
if (swipping)
{
changedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - positionDiff;
changedPosition.z = initialPosition.z;
prevPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = changedPosition;
}
}
}
private bool GetTouchState()
{
if (Input.touchCount > 0 && Input.touchCount < 2)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
return true;
else if (touch.phase == TouchPhase.Moved)
return true;
else if (touch.phase == TouchPhase.Stationary)
return true;
else if (touch.phase == TouchPhase.Canceled)
return false;
else if (touch.phase == TouchPhase.Ended)
return false;
}
else
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_METRO
if (Input.GetMouseButtonDown(0))
return true;
if (Input.GetMouseButton(0))
return true;
else if (Input.GetMouseButtonUp(0))
return false;
#endif
}
return false;
}
private void placeObjectAtProperPosition()
{
if (isCurrentObjectCollidingWithOtherObject())
{
transform.position = initialPosition;
}
}
public void rotateObject()
{
currentRotation = gameObject.transform.eulerAngles;
targetRotation.z = (currentRotation.z + (90 * rotationDirection));
StartCoroutine(objectRotationAnimation());
}
public void rotateObject2()
{
currentRotation = gameObject.transform.eulerAngles;
targetRotation.z = (currentRotation.x + (90 * rotationDirection));
StartCoroutine(objectRotationAnimation());
}
public void rotateObject3()
{
currentRotation = gameObject.transform.eulerAngles;
targetRotation.z = (currentRotation.x + (-90 * rotationDirection));
StartCoroutine(objectRotationAnimation());
}
public void rotateObject4()
{
currentRotation = gameObject.transform.eulerAngles;
targetRotation.z = (currentRotation.z + (-90 * rotationDirection));
StartCoroutine(objectRotationAnimation());
}
IEnumerator objectRotationAnimation()
{
// add rotation step to current rotation.
currentRotation.z += (rotationStep * rotationDirection);
gameObject.transform.eulerAngles = currentRotation;
yield return new WaitForSeconds(0);
if (((int)currentRotation.z >
(int)targetRotation.z && rotationDirection < 0) || // for clockwise
((int)currentRotation.z < (int)targetRotation.z && rotationDirection > 0)) // for anti-clockwise
{
StartCoroutine(objectRotationAnimation());
}
else
{
gameObject.transform.eulerAngles = targetRotation;
if (isCurrentObjectCollidingWithOtherObject())
StartCoroutine(rotateObjectAgain());
else
isRotating = isCurrentObject = false;
}
}
IEnumerator rotateObjectAgain()
{
yield return new WaitForSeconds(0.2f);
rotateObject();
rotateObject2();
rotateObject3();
rotateObject4();
}
public bool isCurrentObjectCollidingWithOtherObject()
{
foreach (GameObject objectToCheck in objectsArray)
{
if (!objectToCheck.name.Equals(gameObject.name))
{
if (gameObject.GetComponent<Collider>().bounds.Intersects(objectToCheck.GetComponent<Collider>().bounds))
return true;
}
}
return false;
}
}
1300 vertical