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 Denaton · May 29, 2014 at 07:17 AM · javascriptraycastrendereryieldline

JS Script only work in Editor

So i have two script using the same Line Renderer, only that one of them will only work in the Editor while i do testing. This Script is the one that will only work in the Editor and when i compile it as a webplayer it stop render the line but the second script still work and they more or less do the same thing.

 var lazerSound : AudioClip;
 var damage = 5;
 var distance = 150.0;
 //var debug = false;
 
 function Start () {
 tmpInWidth = maxWidth;
 }
 
 private var width = 0.0;
 private var charge = false;
 
 private var tParamCharge : float = 0;
 private var tParamDeCharge : float = 0;
 private var tParamPing : float = 0;
 private var tParamPong : float = 0;
 private var pingpong = false;
 private var ping = false;
 private var tmpDesWidth = 0.0;
 private var tmpInWidth = 0.5;
 var speed : float = 0.1;
 var bounce : float = 0.1;
 var maxBonce = 0.6;
 var minBonce = 0.4;
 var maxWidth = 0.5;
 
 function Update () {
     if(global.team == "Human"){
         var line : LineRenderer = gameObject.GetComponent(LineRenderer);
         var hit : RaycastHit;
         if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity)) {
         if(!Input.GetMouseButton(1)){
             line.SetPosition(1, hit.point);
             line.SetPosition(0, transform.position);
         }
         //Debug.DrawRay(transform.position, transform.forward*distance, Color.red);
             if (Input.GetMouseButton(0) && !Input.GetMouseButton(1) && global.energy > 0) {
                 global.energy--;
                 PlayClipAt(lazerSound, transform.position);
                 //line.enabled = true;
                 tParamDeCharge = 0.0;
                 if(!pingpong){
                     tParamCharge += Time.deltaTime * speed;
                     width = Mathf.Lerp(0, tmpInWidth, tParamCharge);
                     if(width == 0.5)
                     pingpong = true;
                 }
                 else{
                     if(ping){
                         tParamPing += Time.deltaTime * bounce;
                         tParamPong = 0.0;
                         width = Mathf.Lerp(minBonce, maxBonce, tParamPing);
                         if(width == maxBonce){
                             ping = false;
                         }
                     }
                     else{
                         tParamPong += Time.deltaTime * bounce;
                         tParamPing = 0.0;
                         width = Mathf.Lerp(minBonce, maxBonce, tParamPong);
                         if(width == maxBonce){
                             ping = true;
                         }
                     }
                 }
                 tmpDesWidth = width;
                 global.debug(""+width);
                 line.SetWidth(width,width);
                 if(hit.transform.tag == "Alien"){
                     hit.transform.GetComponent(ai).life--;
                     global.debug(""+hit.transform.GetComponent(ai).life);
                 }
                 //global.debug("Pew pew");
             }
             else{
                 if(width == 0){
                     //line.enabled = false;
                     tParamCharge = 0.0;
                     tParamDeCharge = 0.0;
                     tmpDesWidth = 0.0;
                     tmpInWidth = maxWidth;
                     pingpong = false;
                 }
                 else{
                     tParamDeCharge += Time.deltaTime * speed;
                     tParamCharge = 0.0;
                     width = Mathf.Lerp(tmpDesWidth, 0, tParamDeCharge);
                     tmpInWidth = width;
                     line.SetWidth(width,width);
                 }
             }
         }
     }
 }
 
 function PlayClipAt(clip: AudioClip, pos: Vector3) {
   var tempGO = GameObject("TempAudio"); // create the temp object
   tempGO.transform.position = pos; // set its position
   tempGO.transform.parent = transform;
   var aSource = tempGO.AddComponent(AudioSource); // add an audio source
   aSource.clip = clip; // define the clip
   aSource.minDistance = 15.0;
   aSource.volume = 0.05;
   // set other aSource properties here, if desired
   aSource.Play(); // start the sound
   Destroy(tempGO, clip.length); // destroy object after clip duration
   return aSource; // return the AudioSource reference
 }

While this script do work!

 var scanBaseTime = 10.0;
 var scanTick = 0.4;
 var scanDistance = 3.0;
 var scanTexture:Texture2D;
 private var scanTime:float;
 private var currScanning:Transform;
 private var scanning = false;
 private var line : LineRenderer;
 var durationEffect : float = 1.0;
 function Update () {
     scan();
 }
 
 function scan(){
     if(global.team == "Human"){
         line = gameObject.GetComponent(LineRenderer);
         if (Input.GetMouseButton(1) && !Input.GetMouseButton(0) && global.energy > 0) {
             var hit : RaycastHit;
             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), hit, scanDistance)) {
                 if (hit.transform.tag == "scan"){
                     if(currScanning != null){
                         if(!scanning){
                             for (var i = 0; i < global.scanned.length; i++) {
                                 if(global.scanned[i].name == currScanning.name){
                                     reset();
                                     return;
                                 }
                             }
                             scanning = true;
                         }
                         else{
                             if (scanTime > 0){
                                 if(!whaitEnergyGenerate)
                                 generateEnergy();
 
                                 scanTime -= Time.deltaTime;
                                 //line.enabled = true;
                                 var lerp : float = Mathf.PingPong (Time.time, durationEffect) / durationEffect;
                                 var extra = Mathf.Lerp (-0.5, 0.5, lerp);
                                 line.SetPosition(0, transform.position);
                                 line.SetPosition(1, hit.point+(transform.up*extra));
                                 line.SetWidth(0.5,5);
                                 Debug.Log("Scanning: "+hit.transform.name);
                             }
                             else{
                                 Debug.Log("Scanned: "+hit.transform.name);
                                 global.scanned.Push(hit.transform);
                                 reset();
                                 return;
                             }
                         }
                     }
                     else{
                         currScanning = hit.transform;
                     }
                 }
                 else{
                     reset();
                 }
             }
             else{
                 reset();
             }
         }
         else{
             reset();
         }
     }
 }
 
 private var whaitEnergyGenerate = false;
 
 function generateEnergy(){
     whaitEnergyGenerate = true;
     yield WaitForSeconds (scanTick);
         global.energy--;
         Debug.Log(global.energy);
     whaitEnergyGenerate = false;
 }
 
 function reset(){
     scanTime = scanBaseTime;
     currScanning = null;
     scanning = false;
     line.SetWidth(0,0);
     //line.enabled = false;
 
 }
 
 function OnGUI () {
     if(scanning){
         GUI.BeginGroup (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 200*(scanTime/scanBaseTime), 20));
             GUI.Label(new Rect (0,0,200,20), scanTexture);
         GUI.EndGroup ();
         GUI.Label(new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10,200,20), "Time: "+ Mathf.CeilToInt(scanTime));
     }
 }


Tried my best to format this, sorry i fail'd.

Comment
Add comment · Show 2
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 fafase · May 29, 2014 at 07:19 AM 0
Share

That is a little too long to read. Consider isolating what is relevant, that would speed up the help.

Also, provide a description of the problem, you tell what is here but what is wrong now?

avatar image Denaton · May 29, 2014 at 07:41 AM 0
Share

The problem is that i have no clue why it only work in the Unity 3D Engine and when i compile it, the only thing that do not function is the first script. I have been looking at it for hours and i cant figure out why it wont work. It plays the sound and drain energy, set positions and change the width. However the renderer wont show in the first script even tho i have done the exact same thing in the second script.

So shorten the scripts to what when i do not know what to short it to :/

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider. 1 Answer

Printing Distance b/w two points using line renderer and Raycast 1 Answer

I want to paint the surface of a mesh depending on if it has been seen by a camera, can this be done with shaders? 0 Answers

Yield WaitForSecond is skipping after the first time. 2 Answers

line renderer question 1 Answer


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