Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Building a Raycast Script for a nursing simulation

Discussion in 'Scripting' started by LelandGMC, Dec 12, 2013.

  1. LelandGMC

    LelandGMC

    Joined:
    Apr 27, 2013
    Posts:
    15
    hello, i need help on the raycast for the functionally for the nursing simulator. I'm a beginner programmer and i'm trying figure out how to make it work for our game.

     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You have not said what isn't working, and you haven't used CODE tags (its the # button in the form)
     
  3. LelandGMC

    LelandGMC

    Joined:
    Apr 27, 2013
    Posts:
    15
    I'm working the raycast to point to the patient's body or at least build functionality on it. I want use a sphere collider to make the body move and Building the raycast would trigger a event or make the patient move or at flash the body of it. Like i said, i'm trying to build a raycast script for nursing simulation where the patient got injured .
     
  4. LelandGMC

    LelandGMC

    Joined:
    Apr 27, 2013
    Posts:
    15
    Code (csharp):
    1. using UnityEngine; using System.Collections;
    2.  
    3. public class Raycast : MonoBehaviour {
    4.  
    5. public GameObject Spherecollider;
    6.  
    7. private RaycastHit hit;
    8.  
    9. float distance = 25.0f;
    10.  
    11. public float power = 25f;
    12.  
    13. // Use this for initialization
    14.  
    15. void Start (){
    16.  
    17. }
    18.  
    19. // Update is called once per frame
    20.  
    21. void Update () {
    22.  
    23. //Left mouse buttton
    24.  
    25. if (Input.GetMouseButtonDown(0))
    26.  
    27. {
    28. Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
    29.  
    30. RaycastHit hitInfo;
    31.  
    32. if(Physics.Raycast(rayOrigin, out hitInfo, distance))
    33.  
    34. { Debug.Log (“Casted a Ray”);
    35.  
    36. Debug.DrawLine(rayOrigin.direction, hitInfo.point);
    37.  
    38. if (hitInfo.rigidbody != null)
    39.  
    40. {
    41.  
    42. //Instantiate(GameObject, hitinfo.point, transform.rotation);
    43.  
    44. //Call out the hit information, Rigidbody, addforceposition menu, direction position of the force should be and add the variable hitInfo.rigidbody.AddForceAtPosition(rayOrigin.dir ection * power, hitInfo.point);
    45.  
    46. //Call Hit info, Transformation, Gameobject,render, material, color } } } } }
    I've apologize if i didn't do it right before, Can you look at now where it make more sense?