Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 HyperNovaGames · Nov 16, 2014 at 10:05 PM · raycastright clickleftmouseclick

Left Click and Right Click Ray-casting.(C#)

I'm trying to make a simple game and was experimenting with ray-cast. If you are skilled with this I would be happy if you can help. Anyway, i'm trying to make it so I can set an object (Munchkin) active when left clicked and when right clicked on another object (DogBed) it will then return set active to nothing, the whole half of my script is ignored.. If you can help please do. - LL

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class PickUpMunchkin : MonoBehaviour {
     public GameObject FPMunchkin;
     
     RaycastHit hit; //Set up the Raycast hit
     
     void Update ()
     {
         Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
 
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(transform.position, fwd, out hit, 5)) //If Munchkin is in front of player but within 5 units of the ray (Hit)
             {
                 if(hit.transform.gameObject.tag == "Munchkin") //Check if Munchkin is there
                 {
                     Destroy(hit.transform.gameObject); //Destry scene munchkin
                     FPMunchkin.SetActive(true);
                     Debug.Log("It Works 1st Part");
             }
 
             if (Input.GetMouseButtonDown(1))
             {
                 if (Physics.Raycast(transform.position, fwd, out hit, 5))
                 {
                     if(hit.transform.gameObject.tag == "DogBed")
                     {
                         FPMunchkin.SetActive(false);
                         Debug.Log("It Works 2nd Part");
                         }
                     }
                 }
             }
         }
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Nov 17, 2014 at 12:51 AM

The {} are all wrong. The right button if is nested inside the left button if.

Note that GetButtonDown will only return true once, when the button is pushed down. So for the second part of the code to be executed both buttons must be pressed down in the same frame. At 60 fps this is a pretty challenging task.

Comment
Add comment · Show 1 · 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
avatar image HyperNovaGames · Nov 17, 2014 at 01:11 AM 0
Share

Thank you so much! Wow I feel stupid to make that mistake. Thank you -LL

avatar image
-1

Answer by rob5300 · Nov 16, 2014 at 11:05 PM

Looking at your code, it should work, but to be sure you can:

  • Make sure there is an 3d collider on the objects. For 2d use Raycast2d. That should be from the Physics2d class.

  • Try using the transform of the main camera instead of the player object as it will of forward from the axis origin which may be below the camera view.

  • Increase the reach distance.

  • Try using Debug.Drawray to see in the scene view where the ray really goes.

Comment
Add comment · Show 1 · 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
avatar image HyperNovaGames · Nov 17, 2014 at 12:38 AM 0
Share

Well I took your advice into consideration, Thanks and I found out the problem was that the second part of the code (the right click) doesn't run. If you know why please help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Inventory GUI 0 Answers

Raycast shoot not working properly 0 Answers

How to point a Ray in the right direction? 1 Answer

Unity Rotate Raycast on Quaternion 1 Answer

Why doesn't Physics.Raycast(ray, RaycastHit, distance) work? 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