- Home /
Raycasting not working on my colliders
Hey all. Been trawling forums, help boards and youtube for hours now and cant figure out my issue. I am fairly new at development. I understand code and what is going on but still need help to build most code. What i am trying to do is this - 50 cards on a screen. On first click the card becomes large. Once its large next click rotates it to the back. Each click from then on rotates the card 180 degrees again. I used an animation for the enlarging of the card and coded the rotation. I used a sprite (its a 2D project) for the front, a different one for the back and attached them both to an empty. The problem i am facing now is that raycasting is not working. No matter where i click, every card becomes large. Any reason why my raycast wont work (not in my script ive attached as i deleted it) or is there another way i can get only specific card that clicked on to become large and only when its clicked on, not anywhere on the screen. Appreciate any feedback. Here is my script.
public class CardManager : MonoBehaviour
{
Animator anim;
public bool isLarge = false;
public int fps = 60;
public float rotateDegreePerSecond = 180;
public bool isFaceUp = false;
const float FLIP_LIMIT_DEGREE = 180f;
float waitTime;
bool isAnimationProcessing = false;
void Start()
{
waitTime = 1.0f / fps;
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
anim.SetTrigger("Active");
isLarge = true;
}
}
void OnMouseDown()
{
if (isAnimationProcessing)
{
return;
}
if (!isLarge)
{
return;
}
StartCoroutine(Flip());
}
IEnumerator Flip()
{
isAnimationProcessing = true;
bool done = false;
while (!done)
{
float degree = rotateDegreePerSecond * Time.deltaTime;
if (isFaceUp)
{
degree = -degree;
}
transform.Rotate(new Vector3(0, degree, 0));
if (FLIP_LIMIT_DEGREE < transform.eulerAngles.y)
{
transform.Rotate(new Vector3(0, -degree, 0));
done = true;
}
yield return new WaitForSeconds(waitTime);
}
isFaceUp = !isFaceUp;
isAnimationProcessing = false;
}
}
Answer by Klarzahs · Feb 18, 2019 at 12:18 PM
Hi,
Assuming you attached your CardManager component to all card gameobjects: Update() will be called on each of the card, including the MouseButtonDown check. Now, if you click, each card will independently check this click again, register it and flip.
Regarding your raycast: As you look down towards your cards, you can raycast "straight down" from your camera, and use your mouse position as origin of the ray.
You can translate the mouse position to world space with Camera.ScreenToWorldPoint(). The direction of the ray is camera.forward.
After the raycast you should get only one object, where you can manually call flip(). Also, you should have a global class that handles the raycasting, not one on every card. Otherwise you'll flip the same card lots of times ;)
Your answer
Follow this Question
Related Questions
2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers
Detecting Click on Object Issue 1 Answer
Ray Casting in 2D 1 Answer
2D Collider larger than specified 0 Answers
How to count numbers on onmousedown? 1 Answer