Search Unity

Could Unity Update the Raycast tutorial.

Discussion in 'Community Learning & Teaching' started by NintendoMaster00, Jan 23, 2016.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Could you guys a unity Update the Raycast tutorial, it is from a version 3 of unity and that does not work with unity 5.
     
  2. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    We have a tutorial for Raycasts using Unity 5. You can find it here:
     
    NintendoMaster00 likes this.
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Thank you very much!
     
  4. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Is there a way in unity 5 to raycast.tag == "Tagname" ?


    I am trying to do something like this:

    Code (CSharp):
    1. RaycastHit Hit;
    2.         Ray ray = new Ray(transform.position, Vector3.up);
    3.         //Debug.DrawLine(transform.position, transform.position + Vector3.up * RayLength, Color.red); // Shows where the raycast is in the inspector
    4.         if(Physics.Raycast(ray, out Hit, RayLength))
    5.         {
    6.             if(Physics.Raycast(ray, out Hit.collider.gameObject.CompareTag("Rock"), RayLength))
    7.             {
    8.                 RockInteract();
    9.             }
    10.         }
     
  5. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    You are creating two Raycasts when you should only be creating one. Inside the first 'if' block, you can check to see if the object you hit has the tag you are looking for by checking
    Code (CSharp):
    1. if (Hit.collider.tag.Equals("YourTagHere"))
    2. {
    3.      //do stuff
    4. }
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you point me to this tutorial and tell me what's not working about it?
     
  7. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

    This is the code that was given
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ParachuteOpening : MonoBehaviour {
    5.  
    6.     public GameObject parachute;
    7.     public float parachuteEffectiveness;
    8.     public float deploymentHeight;
    9.  
    10.     private bool deployed;
    11.    
    12.     void Update ()
    13.     {
    14.         RaycastHit hit;
    15.         Ray landingRay = new Ray(transform.position, Vector3.down);
    16.  
    17.         if(!deployed)
    18.         {
    19.             if(Physics.Raycast(landingRay, out hit, deploymentHeight))
    20.             {
    21.                 if(hit.collider.tag == "Environment")
    22.                 {
    23.                     DeployParachute();
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
    29.     void DeployParachute()
    30.     {
    31.         deployed = true;
    32.         rigidbody.drag = parachuteEffectiveness;
    33.         parachute.animation.Play("openChute");
    34.     }
    35.  
    36.     void OnCOllisionEnter()
    37.     {
    38.         parachute.animation.Play("closeChute");
    39.     }
    40. }
    41.  

    first of all this happens Unity.JPG

    after that the code is like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ParachuteOpening : MonoBehaviour {
    5.  
    6.     public GameObject parachute;
    7.     public float parachuteEffectiveness;
    8.     public float deploymentHeight;
    9.  
    10.     private bool deployed;
    11.    
    12.     void Update ()
    13.     {
    14.         RaycastHit hit;
    15.         Ray landingRay = new Ray(transform.position, Vector3.down);
    16.  
    17.         if(!deployed)
    18.         {
    19.             if(Physics.Raycast(landingRay, out hit, deploymentHeight))
    20.             {
    21.                 if(hit.collider.tag == "Environment")
    22.                 {
    23.                     DeployParachute();
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
    29.     void DeployParachute()
    30.     {
    31.         deployed = true;
    32.         GetComponent<Rigidbody>().drag = parachuteEffectiveness;
    33.         parachute.GetComponent<Animation>().Play("openChute");
    34.     }
    35.  
    36.     void OnCOllisionEnter()
    37.     {
    38.         parachute.GetComponent<Animation>().Play("closeChute");
    39.     }
    40. }
    41.  
    all it is that you have to do the API update but other than that it still works
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  9. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Be aware, however, that the core concepts are still valid.

    The old v3.5 code used our deprecated shortcuts:
    Code (csharp):
    1. rigidbody.drag = parachuteEffectiveness;
    2. parachute.animation.Play("openChute");
    3. parachute.animation.Play("closeChute");
    and the new code simply updates these to:
    Code (csharp):
    1. GetComponent<Rigidbody>().drag = parachuteEffectiveness;
    2. parachute.GetComponent<Animation>().Play("openChute");
    3. parachute.GetComponent<Animation>().Play("closeChute");
    The simple fix would be to declare the following variables:
    Code (csharp):
    1. private Rigidbody rb;
    2. private Animation anim;
    ... and add to a Start function:
    Code (csharp):
    1. rb = GetComponent<Rigidbody>();
    2. anim = GetComponent<Animation>();
    Then you could use rb and anim directly as references:
    Code (csharp):
    1. rb.drag = parachuteEffectiveness;
    2. parachute.anim.Play("openChute");
    3. parachute.anim.Play("closeChute");
     
    NintendoMaster00 likes this.