- Home /
Problem is not reproducible or outdated
Flip over an object (smooth transition)
Simple enough, lift the card off the table flip it over 180 degrees and place it back down, by clicking on it. I can do the raycasts on click but i just can't think of how to flip it over. i was thinking a while loop in a coroutine? help :3
Edit:
So I do not want to use animations I spent some time and come up with the rotation code and just need now to make it move up a bit after that.
using UnityEngine;
using System.Collections;
public class FlipCard : MonoBehaviour {
private bool m_CanFlip = true;
private bool m_Match = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
Debug.Log(tag);
if(hit.collider.CompareTag("Card"))
{
StartCoroutine(CardFlip());
}
}
}
}
private IEnumerator CardFlip()
{
Quaternion angle = Quaternion.Euler(0, -180, 0); Quaternion reverce = Quaternion.Euler(0, 0, 0);
if(m_CanFlip == true)
{
while(Quaternion.Angle(transform.rotation, angle)>5)
{
transform.rotation = Quaternion.Slerp(transform.rotation, angle, Time.deltaTime * 5);
yield return new WaitForEndOfFrame();
}
transform.rotation = angle;
m_CanFlip = false;
}
if(m_Match == false)
{
yield return new WaitForSeconds(1.5f);
while(Quaternion.Angle(transform.rotation, reverce)>5)
{
transform.rotation = Quaternion.Slerp(transform.rotation, reverce, Time.deltaTime * 5);
yield return new WaitForEndOfFrame();
}
transform.rotation = reverce;
m_CanFlip = true;
}
}
}
So what this does is on click it will flip the card and then after some time it flip back over is the Bool m_Match is false (as later there will be more cards and I will compare there tags to see if they match) but now I need this to also raise the card very slightly on click before it flips. Thanks!
There are many posts on smooth movement and smooth rotation. You might start with this script in the Wiki:
http://wiki.unity3d.com/index.php?title=$$anonymous$$oveObject
also, try using the animation system. if the card should always perform the same action and doesn't need to be generative or react to the environment while it's flipping over, animations are your simple go-to for this sort of thing.
Im looking at using Quaternion in a while loop using a co-routine?
Answer by zombience · Oct 22, 2013 at 04:48 PM
If you are committed to using strict code and staying away from animations, here is a simple script that will lift an object up and put it back down in place, rotated 180.
I maintain that, from what I can see, it seems like animations would be your best bet.
Place the following code on an object. in the inspector, drag the "progress" value up and watch as your object lifts up, rotates, and moves back down.
using UnityEngine;
using System.Collections;
public class Flip : MonoBehaviour
{
public float progress = 0;
public float height = 5f;
private Vector3 origin;
private Vector3 origRot;
private Vector3 flipRot;
void Start ()
{
origin = transform.position;
flipRot = origRot = transform.rotation.eulerAngles;
flipRot.z += 180;
}
void Update ()
{
progress = Mathf.Clamp(progress, 0, 90);
Vector3 pos = origin;
pos.y = (Mathf.Sin(Mathf.Deg2Rad * progress) * Mathf.Cos(Mathf.Deg2Rad * progress) * height) + origin.y;
transform.position = pos; // handle moving upwards
transform.rotation = Quaternion.Lerp(Quaternion.Euler(origRot), Quaternion.Euler(flipRot), progress / 90); // handle rotation
}
}
Answer by haim96 · Oct 22, 2013 at 01:04 PM
i had similar issue with flipping my airplane. i ended up with creating animation clip and play it when it needed. it works really great because i could add realistic flipping movement.
make sure you change animation type to legacy in the imported mesh (if any) create new animation clip, adjust the curves you need and play the animation with code.
check this for more info: http://docs.unity3d.com/Documentation/Manual/AnimationEditorGuide40.html
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotate a Canvas element over time 0 Answers
Distribute terrain in zones 3 Answers