Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nizzle83 · Feb 17, 2019 at 09:29 AM · 2draycastonmousedowncards

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

230 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges