Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast does not work as intended and RotateAround does give error and does not work wehn tested.

Discussion in 'Scripting' started by BottomGames, Jul 29, 2014.

  1. BottomGames

    BottomGames

    Joined:
    Jun 23, 2012
    Posts:
    28
    So I am attempting to create a door that rotates around a hinge, when a button is activated through Raycast it does not print the "Debug.Log("Button pressed")" to the console at all. Is there any other way i can improve upon the raycast method to guarantee a raycast hit, like using an empty from which the raycast originates. Then the RotateAround does not give code error, yet is does not rotate around the object designated as center/point of origin. Is there something wrong in my code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Switch : MonoBehaviour {
    5.     public Transform door;
    6.     public Transform doorHinge;
    7.     public float angle = 20;
    8.  
    9.  
    10.     void update () {
    11.         RaycastHit hit;
    12.         if (Input.GetKeyDown(KeyCode.E))
    13.         {
    14.             if (Physics.Raycast(transform.position, transform.forward, out hit, 2.0F))
    15.             {
    16.                 if(hit.transform.tag == "Button")
    17.                 {
    18.                     door.RotateAround(doorHinge.position, Vector3.up, angle * Time.deltaTime);
    19.                     Debug.Log("Button pressed");
    20.                 }
    21.             }
    22.         }
    23.     }
    24. }
    Please point in right direction or help with code and thanks in advance for the help.
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    try hit.transform.gameObject.tag == "Button"

    or just hit.gameObject.tag
     
    Last edited: Jul 29, 2014
  3. BottomGames

    BottomGames

    Joined:
    Jun 23, 2012
    Posts:
    28
    so i tried it with hit.transform.gameObject.tag and still no result. With hit.gameObject.tag it gives an error. Is there any documentation or tutorials that shows some techniques in using raycast. I tried looking and anything I find does not thoroughly explain it. It just shows me what I know. Sadly this http://docs.unity3d.com/ScriptReference/Physics.Raycast.html its not helping either.
     
  4. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    how about

    Code (CSharp):
    1. if(Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 2.0f))
    2. {
    3. if(hit.transform.gameObject.tag == "Button")
    4.                 {
    5.                     door.RotateAround(doorHinge.position, Vector3.up, angle * Time.deltaTime);
    6.                     Debug.Log("Button pressed");
    7.                 }
    8. }
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    what is this script attached to? and can you explain in words what you are trying to achieve with the raycast? (check the player is near enough to the button?)
     
  6. BottomGames

    BottomGames

    Joined:
    Jun 23, 2012
    Posts:
    28
    Ok I have the script attached to the player prefab(I also tried attaching it to the button). I have the objects that I want to rotate around assigned to the script. What I'm trying to achieve with the raycast is to have the player look at an object tagged with button and if they press a certain key it opens the door a few degrees(degrees controlled through the script). Hopefully that makes sense, I'm prone to have grammar errors. Thanks in advance.
     
  7. BottomGames

    BottomGames

    Joined:
    Jun 23, 2012
    Posts:
    28
    Does anyone have a sure way to create a button. Something like a switch.