Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Solist · Aug 27, 2014 at 12:46 PM · raycasttagsactivationdistance check

I want to setup a raycast that is going forward from my character that allows me to left click on a cube with the tag "npc" and open up a gui window (in C#)

This is for a third person camera view from behind the character/over the shoulder. My raycast is supposed to be aimed forward from my character at about mid height so that if I come across a merchant I can left click on him and it will ignore my character and check whether the npc is a certain distance from me and whether it possesses the tag "npc" which will open up the gui window with all the goods to sell.

there are still a few variables floating around in this script because i'm still editing :P.

any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class ClickHandler : MonoBehaviour {
 
     public Transform playerTarget;
     public Transform activateRange;
     public float chatDist;
     public string tagCheck = "Npc";
     bool checkAllTags = false;
     public Texture2D chatBG;
     public Texture2D shopBG;
     public bool foundHit;
     public bool shop = false;
     public bool endShop = false;
     public bool talking = false;
     public TextAsset dialogueText;
     string displayText;
     public GUIStyle textStyle = new GUIStyle();
         
     // Use this for initialization
     void Start () {
         foundHit = false;
         checkAllTags = false;
         talking = false;
         displayText = dialogueText.text;
         textStyle.fontSize = Screen.height / 60;
         textStyle.normal.textColor = Color.white;
     }
 
     
     // Update is called once per frame
     void Update () {
 
         RaycastHit hit;
 
         if (Input.GetMouseButtonDown(0)) {
             foundHit = Physics.Raycast (playerTarget.transform.position, activateRange.transform.position, out hit);
                 
                 if (foundHit && hit.transform.tag != "npc") {
                     foundHit = false;
                 }
                 if (foundHit && hit.transform.tag == "npc") {
                     talking = !talking;
                 }
         }
     }
 
         
         
     void OnGUI(){
         if (talking == true) {
 
             GUI.DrawTexture (new Rect(Screen.width/2 - Screen.width / 4, Screen.height / 11, Screen.width / 5, Screen.height / 3), chatBG);
 
             GUI.Box (new Rect(Screen.width / 4 + Screen.width / 60, Screen.height / 8, Screen.width / 5, Screen.height / 4), displayText, textStyle);
 
             GUI.Button (new Rect(Screen.width / 2 - Screen.width / 5 - Screen.width / 30, Screen.height / 2 - Screen.height / 7, Screen.width / 14, Screen.height / 32), "Thankyou!");
 
             if (GUI.Button (new Rect(Screen.width / 2 - Screen.width / 7 + Screen.width / 150, Screen.height / 2 - Screen.height / 7, Screen.width / 14, Screen.height / 32), "Go to hell!")) {
                     talking = false;
             }
         }
     }
 }


what I have found is that i go into game and i move back a rather far distance and it finally checks the foundHit but wont activate foundHit if i'm any closer or further. another thing I've noticed is that the hit.transform.tag == "npc" isn't picking up the object tag.

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

Answer by killerstreak · Aug 27, 2014 at 04:22 PM

not even gonna read the code here because I am novice (or lazy). but wouldnt a trigger sphere around the merchant be a better idea? like when you walk inside of it.. you can click it..

like this script could be attached to the merchant

 var clickable : boolean;
 
 function OnTriggerStay3D(other : Collider3D)
 {
    if(other.gameObject.tag=="Player")
    {
    clickable == "true";
    }
    else
    {
    clickable == "false";
    }
 }
 
 function OnMouseDown()
 {
 if(clickable == "true")
  {
  //open gui window
  }
 }

 function Update()
 {
 if(clickable == "false")
  {
  //close gui window
  }
 }


alot less code ;)

sry for any syntax mistakes I might have made

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
avatar image
0

Answer by Cherno · Aug 28, 2014 at 09:27 AM

This should do the trick: It offsets both origin and target vector of the ray by 0.5 upwards.

 foundHit = Physics.Raycast (playerTarget.transform.position + Vector3.up * 0.5f, playerTarget.transform.forward + playerTarget.transform.position.up * 0.5f, out hit);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set distance away from an object 0 Answers

[Problem] Cant activate 2D objects with touch? 1 Answer

detecting an object distance behind an other one with a raycast 1 Answer

Raycast that ignores object with certain tags? 2 Answers

Raycast isn't working when with tag 2 Answers


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