I'm trying to use the ang variable to check the degrees and make the name change. But it's never get to 180 degrees only close to it.
I want that while the transform is rotating using the StartCoroutine when the object rotated 180 degrees then make the name change.
Not when the transform is at 180 degrees but after the object rotated 180 degrees then change the name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnMouseOverEvent : MonoBehaviour
{
public float speed = 5f;
public float distanceToMove = 1f;
public bool goForward;
public Vector3 startPos;
public Vector3 endPos;
private bool isRotating = false;
private Vector3 lastFwd;
private float curAngleX = 0;
private void Start()
{
lastFwd = transform.forward;
startPos = transform.position;
endPos = transform.position - Vector3.forward * distanceToMove;
}
void Update()
{
if (goForward && isRotating == false)
{
transform.position = Vector3.MoveTowards(transform.position, endPos, speed * Time.deltaTime);
}
else if (isRotating == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPos, speed * Time.deltaTime);
}
var curFwd = transform.forward;
// measure the angle rotated since last frame:
var ang = Vector3.Angle(curFwd, lastFwd);
if (ang == 180f)
transform.GetChild(0).GetComponent<TextMesh>().text = "Changed";
}
private void OnMouseOver()
{
goForward = true;
}
private void OnMouseExit()
{
goForward = false;
}
private void OnMouseDown()
{
if (isRotating == false && transform.name == "Options")
StartCoroutine(Rotate(5));
}
IEnumerator Rotate(float duration)
{
Quaternion startRot = transform.rotation;
float t = 0.0f;
while (t < duration)
{
isRotating = true;
t += Time.deltaTime;
transform.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up); //or transform.right if you want it to be locally based
yield return null;
}
transform.rotation = startRot;
isRotating = false;
}
}
Update :
I'm not sure if it's the right solution but this is working :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnMouseOverEvent : MonoBehaviour
{
public float speed = 5f;
public float distanceToMove = 1f;
public bool goForward;
public Vector3 startPos;
public Vector3 endPos;
private bool isRotating = false;
private Vector3 lastFwd;
private float curAngleX = 0;
private void Start()
{
lastFwd = transform.forward;
startPos = transform.position;
endPos = transform.position - Vector3.forward * distanceToMove;
}
void Update()
{
if (goForward && isRotating == false)
{
transform.position = Vector3.MoveTowards(transform.position, endPos, speed * Time.deltaTime);
}
else if (isRotating == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPos, speed * Time.deltaTime);
}
var curFwd = transform.forward;
// measure the angle rotated since last frame:
var ang = Vector3.Angle(curFwd, lastFwd);
if (myApproximation(ang, 110f, 10f) == true)
transform.GetChild(0).GetComponent<TextMesh>().text = "Changed";
}
private bool myApproximation(float a, float b, float tolerance)
{
return (Mathf.Abs(a - b) < tolerance);
}
private void OnMouseOver()
{
goForward = true;
}
private void OnMouseExit()
{
goForward = false;
}
private void OnMouseDown()
{
if (isRotating == false && transform.name == "Options")
StartCoroutine(Rotate(5));
}
IEnumerator Rotate(float duration)
{
Quaternion startRot = transform.rotation;
float t = 0.0f;
while (t < duration)
{
isRotating = true;
t += Time.deltaTime;
transform.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up); //or transform.right if you want it to be locally based
yield return null;
}
transform.rotation = startRot;
isRotating = false;
}
}
I'm using my own Approximation method (myApproximation) and I also changed back the lines :
lastFwd = transform.forward;
And
var curFwd = transform.forward;
To be forward it was up