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

Can this prefab raycast script be recreated in javascript?

Discussion in 'Scripting' started by Datester35, Nov 20, 2014.

  1. Datester35

    Datester35

    Joined:
    Mar 30, 2013
    Posts:
    9
    Hello, i have a script that i have been trying to recreate in javascript, but it keeps giving me a tremendous amount of errors. Is it eve possible to convert this script into javascript?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class test : MonoBehaviour {
    4.    
    5.      Ray ray;
    6.      RaycastHit hit;
    7.      public GameObject prefab;
    8.    
    9.      // Use this for initialization
    10.      void Start () {
    11.        
    12.        
    13.        
    14.        
    15.      }
    16.    
    17.      // Update is called once per frame
    18.      void Update () {
    19.        
    20.          ray=Camera.main.ScreenPointToRay(Input.mousePosition);
    21.        
    22.          if(Physics.Raycast(ray,out hit))
    23.          {
    24.            
    25.              if(Input.GetKey(KeyCode.Mouse0))
    26.              {
    27.                  GameObject obj=Instantiate(prefab,new Vector3(hit.point.x,hit.point.y,hit.point.z), Quaternion.identity) as GameObject;
    28.                
    29.              }
    30.            
    31.          }
    32.        
    33.        
    34.        
    35.      }
    36. }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yes, possible
    Why would you even downgrade to js tho
     
  3. Datester35

    Datester35

    Joined:
    Mar 30, 2013
    Posts:
    9
    Because i am terrible with csharp. If you can do it, please do, because i keep getting errors. Also, it's upgrading to javascript, not downgrading.
     
    Last edited: Nov 20, 2014
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    This is basic Unity programming. You should try using the documentation to get this done, instead of copying from others.
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    js has little or no advantages over c# execpt for being marginally more user friendly; and has some outright downsides