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 davidflynn2 · Jun 03, 2013 at 01:42 PM · c#convert

Lightning Script Js To C# Conversion errors

I have been working on converting a JS to C# for a while now and have been having some problems with this one. With some help I have managed to get it narrowed down to one line causing a problem now. My problem is I cant figure out how to fix it. I have found a site that trys to convert it for you and it was no help on this part.

The errors are ass follows.

Assets/Lightning.cs(26,8): error CS1502: The best overloaded method match for Lightning.Randomize(UnityEngine.Vector3, float)' has some invalid arguments Assets/Lightning.cs(26,8): error CS1503: Argument #2' cannot convert double' expression to type float'

Assets/Lightning.cs(27,16): error CS1502: The best overloaded method match for UnityEngine.Random.Range(float, float)' has some invalid arguments Assets/Lightning.cs(27,16): error CS1503: Argument #1' cannot convert double' expression to type float'

Assets/Lightning.cs(37,23): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Here is the C# version of the script.

 using UnityEngine;
 using System.Collections;
 
 public class Lightning : MonoBehaviour {
 
 public GameObject target;
 public LineRenderer LR;
 public double arcLength = 2.0;
 public double arcVariation = 2.0;
 public double inaccuracy = 1.0;
 public Vector3 fwd;
 
 void Update()
 {
 
 Vector3 lastPoint = transform.position;
 
 int i = 1;
 
 LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform   
  while (Vector3.Distance(target.transform.position, lastPoint) >.5) 
  {//was the last arc not touching the target?            
  LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer          
  fwd = target.transform.position - lastPoint;//gives the direction to our target from the end of the last arc20.         
  fwd.Normalize();//makes the direction to scale         
  fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though      
  fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform    
  fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends     
  LR.SetPosition(i, fwd);//this tells the line renderer where to draw to25.         
  i++;         
  lastPoint = fwd;//so we know where we are starting from for the next arc        
  }
  }
      private Vector3 Randomize(Vector3 v3 ,   float inaccuracy2)
     {
         
         v3 += Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2; 
           v3.Normalize(); 
            return v3; 
         
     }
 }

This is the JS version I converted it from.

 var target : GameObject;
 var LR : LineRenderer;
 var arcLength = 2.0;
 var arcVariation = 2.0;
 var inaccuracy = 1.0;
 
 function Update() {
     var lastPoint = transform.position;
     var i = 1;
     LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform
     while (Vector3.Distance(target.transform.position, lastPoint) >.5) {//was the last arc not touching the target? 
             LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer
             var fwd = target.transform.position - lastPoint;//gives the direction to our target from the end of the last arc
             fwd.Normalize();//makes the direction to scale
             fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though
             fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform
             fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends
             LR.SetPosition(i, fwd);//this tells the line renderer where to draw to
             i++;
             lastPoint = fwd;//so we know where we are starting from for the next arc
          }
 }
 
 function Randomize (v3 : Vector3, inaccuracy2 : float) { 
    v3 += Vector3(Random.Range(-1.0, 1.0), Random.Range(-1.0, 1.0), Random.Range(-1.0, 1.0)) * inaccuracy2; 
    v3.Normalize(); 
    return v3; 
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Chronos-L · Jun 03, 2013 at 02:00 PM

You should know that by default, 2.0 is considered to be a float in uJS. So you should change all the related numbers to a float when you convert it to C#. By the way, if you read the error carefully, you will know how to fix it:

  • Assets/Lightning.cs(26,8): error CS1502: The best overloaded method match for Lightning.Randomize(UnityEngine.Vector3, float)' has some invalid arguments**: One or more of the parameter is not of the correct type - **Assets/Lightning.cs(26,8): error CS1503: Argument #2' cannot convertdouble' expression to type float': Argument/Parameter #2 should be a double and the current value for the said parameter cannot be converted to a double

    Correction

    using UnityEngine; using System.Collections;

    public class Lightning : MonoBehaviour {

       public GameObject target;
         public LineRenderer LR;
         
         //Just change all these to float
         public float arcLength = 2.0f;
         public float arcVariation = 2.0f;
         public float inaccuracy = 1.0f;
         
         public Vector3 fwd;
          
         void Update()
         {
             ... 
         }
     
         private Vector3 Randomize(Vector3 v3 ,   float inaccuracy2)
         {
             //You need the new keyword in C#
             v3 += new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2; 
             v3.Normalize(); 
             return v3; 
     
         }
     }
    
Comment
Add comment · Share
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
1

Answer by SubatomicHero · Jun 03, 2013 at 02:00 PM

You need to change all your variables to floats like so:

 public float arcLength = 2.0f;
 public float arcVariation = 2.0f;
 public float inaccuracy = 1.0f;

Hope this helps.

Comment
Add comment · Share
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

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Please Help Convert js to c# 1 Answer

Converting to C# 1 Answer

Spawn Meteors At random location C# 1 Answer

how can convert this in js? 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