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 /
This question was closed Dec 12, 2013 at 02:25 PM by NicoLocke for the following reason:

Too subjective and argumentative

avatar image
0
Question by NicoLocke · Dec 10, 2013 at 04:08 PM · c#javascriptconvert

Anyone able to convert this script to c#?

Script is in javascript, fairly new to programming so would anyone be able to convert this to c#?

 /********Main Objects***********/
 
 var targetItem : GameObject;
 var GUICamera : Camera;
 var ambient : GameObject;
 
 
 /********Rotation Variables*********/
 var rotationRate : float = 1.0;
 private var wasRotating;
 
 /************Scrolling inertia variables************/
 private var scrollPosition : Vector2 = Vector2.zero;
 private var scrollVelocity : float = 0;
 private var timeTouchPhaseEnded: float;
 private var inertiaDuration : float = 0.5f;
 
 private var itemInertiaDuration : float = 1.0f;
 private var itemTimeTouchPhaseEnded: float;
 private var rotateVelocityX : float = 0;
 private var rotateVelocityY : float = 0;
 
 
 var hit: RaycastHit;
 
 private var layerMask = (1 <<  8) | (1 << 2);
 
 
 
 function Start()
 {
     layerMask =~ layerMask;    
 }
 
 function FixedUpdate()
 {
     
     if (Input.touchCount > 0) 
     {        //    If there are touches...
             var theTouch : Touch = Input.GetTouch (0);        //    Cache Touch (0)
             
             var ray = Camera.main.ScreenPointToRay(theTouch.position);
             //var GUIRay = GUICamera.ScreenPointToRay(theTouch.position);
             
                 
              if(Physics.Raycast(ray,hit,50,layerMask))
              {    
 
                                                if(Input.touchCount == 1)
                         {
                             
                             if (theTouch.phase == TouchPhase.Began) 
                              {
                                  wasRotating = false;    
                              }        
                              
                              if (theTouch.phase == TouchPhase.Moved) 
                              {
                                   
                                  targetItem.transform.Rotate(theTouch.deltaPosition.y * rotationRate, -theTouch.deltaPosition.x * rotationRate,0,Space.World);
                                  wasRotating = true;
                              }        
              
                              if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Canceled) 
                              {
                                  if(wasRotating==true)
                                  {
                                      if(Mathf.Abs(theTouch.deltaPosition.x) >=10)
                                      {
                                          rotateVelocityX = theTouch.deltaPosition.x / theTouch.deltaTime;
                                        }
                                        if(Mathf.Abs(theTouch.deltaPosition.y) >=10)
                                      {
                                          rotateVelocityY = theTouch.deltaPosition.y / theTouch.deltaTime;
                                        }    
                                  itemTimeTouchPhaseEnded = Time.time;
                                  }
                                                    }
                         }
             }
 
 
             
                         
             
     }
 /******************** Inertia code ******************************/
     if(Input.touchCount == 0 )
     {
                 if(scrollVelocity != 0.0)
              {
                  //slowing down
                  var t : float = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
                  var frameVelocity : float = Mathf.Lerp(scrollVelocity, 0 , t);
                          
                  scrollPosition.x -= frameVelocity * Time.deltaTime;
                          
                  if(t >= inertiaDuration)
                      scrollVelocity = 0.0f;
                          
                          
              }    
          
                 if(rotateVelocityX != 0.0f || rotateVelocityY != 0.0f)
              {
                  //slowing down
                  var ty : float = (Time.time - itemTimeTouchPhaseEnded) / itemInertiaDuration;
                  var XVelocity : float = Mathf.Lerp(rotateVelocityX, 0 , ty);
                  var YVelocity : float = Mathf.Lerp(rotateVelocityY, 0 , ty);     
                  
                          
                  if(ty >= inertiaDuration)
                  {
                      rotateVelocityX = 0.0f;
                      rotateVelocityY = 0.0f;
                      
                  }    
                  targetItem.transform.Rotate(YVelocity*Time.deltaTime * rotationRate, -XVelocity * Time.deltaTime * rotationRate,0,Space.World);        
                          
              }    
         
         
       }    
 }

All the best,

Nico.

Comment
Add comment · Show 25
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 Jamora · Dec 11, 2013 at 09:30 AM 11
Share

I downvoted because converting scripts from UnityScript to C# (and vice versa) is such a great learning opportunity, that especially people new to program$$anonymous$$g should do it. The act of converting code to other languages is a great exercise in syntax. So you asking for others to do it for you means, in my opinion, that you are lazy and not interested in learning even the correct syntax of a program$$anonymous$$g language.

avatar image Exalia · Dec 12, 2013 at 12:07 PM 2
Share

You both have good points, at the end of the day The question was answered, by the person who created it.

Both of you got what you wanted so I don't believe a down vote was necessary :\

avatar image haim96 · Dec 12, 2013 at 12:28 PM 2
Share

well... i +1 you because it seems to be useful code for me and you also did the translation for me! :) thanks....

avatar image NicoLocke · Dec 12, 2013 at 12:32 PM 2
Share

I spend everyday trying to improve as a programmer - which is a role that I've come into and not by any means my area of expertise. So to say that I'm lazy and not interested is ridiculous.

avatar image Exalia · Dec 12, 2013 at 12:45 PM 3
Share

If i'm completely honest, a large majority of programmers are socially inept, don't take what they say personally because it is likely they haven't thought about the other person's emotions when writing their message.

There are also going to be a lot of people who are judgmental or looking to show off their 'Epeen'.

This is the internet so prepare for the worst and hope for the best :) Ignore the people who are simply not helpful and embrace the people that are.

Show more comments

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by StormSabotage · Dec 10, 2013 at 04:23 PM

Take ;)

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     /********Main Objects***********/
     
     public GameObject targetItem;
     public Camera GUICamera;
     public GameObject ambient;
     
     
     /********Rotation Variables*********/
     private float rotationRate = 1.0f;
     private bool wasRotating;
     
     /************Scrolling inertia variables************/
     private Vector2 scrollPosition = Vector2.zero;
     private float scrollVelocity = 0;
     private float timeTouchPhaseEnded;
     private float inertiaDuration = 0.5f;
     
     private float itemInertiaDuration = 1.0f;
     private float itemTimeTouchPhaseEnded;
     private float rotateVelocityX = 0;
     private float rotateVelocityY = 0;
     
     
     private RaycastHit hit;
     
     private LayerMask layerMask = (1 <<  8) | (1 << 2);
 
     void Start(){
         layerMask =~ layerMask;    
     }
 
     void FixedUpdate(){
 
         if (Input.touchCount > 0) {
             //  If there are touches...
             Touch theTouch = Input.GetTouch (0);     //   Cache Touch (0)
             
             Ray ray = Camera.main.ScreenPointToRay(theTouch.position);
             
             if(Physics.Raycast(ray,out hit,50,layerMask)){
                 if(Input.touchCount == 1){
 
                     if (theTouch.phase == TouchPhase.Began){
                         wasRotating = false; 
                     }     
                     
                     if (theTouch.phase == TouchPhase.Moved){
                         targetItem.transform.Rotate(theTouch.deltaPosition.y * rotationRate, -theTouch.deltaPosition.x * rotationRate,0,Space.World);
                         wasRotating = true;
                     }     
                     
                     if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Canceled){
                         if(wasRotating == true){
                             if(Mathf.Abs(theTouch.deltaPosition.x) >=10){
                                 rotateVelocityX = theTouch.deltaPosition.x / theTouch.deltaTime;
                             }
                             if(Mathf.Abs(theTouch.deltaPosition.y) >=10){
                                 rotateVelocityY = theTouch.deltaPosition.y / theTouch.deltaTime;
                             } 
                             itemTimeTouchPhaseEnded = Time.time;
                         }
                     }
                 }
             }
         }
 
         /******************** Inertia code ******************************/
         if(Input.touchCount == 0 ){
             if(scrollVelocity != 0.0){
                 //slowing down
                 float t = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
                 float frameVelocity = Mathf.Lerp(scrollVelocity, 0 , t);
                 
                 scrollPosition.x -= frameVelocity * Time.deltaTime;
                 
                 if(t >= inertiaDuration){
                     scrollVelocity = 0.0f;
                 }
                 
                 
             } 
             
             if(rotateVelocityX != 0.0f || rotateVelocityY != 0.0f){
                 //slowing down
                 float ty = (Time.time - itemTimeTouchPhaseEnded) / itemInertiaDuration;
                 float XVelocity = Mathf.Lerp(rotateVelocityX, 0 , ty);
                 float YVelocity = Mathf.Lerp(rotateVelocityY, 0 , ty);
                 
                 if(ty >= inertiaDuration){
                     rotateVelocityX = 0.0f;
                     rotateVelocityY = 0.0f;    
                 }    
                 targetItem.transform.Rotate(YVelocity*Time.deltaTime * rotationRate, -XVelocity * Time.deltaTime * rotationRate,0,Space.World);   
                 
             } 
         } 
     }
 }
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
2

Answer by Unity_gamer · Dec 12, 2013 at 01:20 PM

People who have less experience in programming could use this .It will help you to convert Java script to C# !!(Or If you wanted to learn, you try by yourself and then you could use this link to check your code).

Thanks

Comment
Add comment · Show 1 · 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 sdgd · Dec 12, 2013 at 03:48 PM 1
Share

this answer is the BEST in my opinion I'm sad that noone in comments figured he needs JS to C# converter.

and @Jamora I'd downvote your comment becuse you weren't really helpfull.

I have very good exp with program$$anonymous$$g till now but I still suck at reading JS. but I've learned how to convert with the tool but still can't without it.

you just can't expect that from new programmers.

any way +1.

avatar image
1

Answer by Exalia · Dec 10, 2013 at 04:14 PM

Here's the variables in C# as a starting point

 /********Main Objects***********/
  
 public GameObject targetItem;
 public Camera GUICamera;
 public GameObject ambient;
  
  
 /********Rotation Variables*********/
 public float rotationRate = 1.0;
 private bool wasRotating;
  
 /************Scrolling inertia variables************/
 private Vector2 scrollPosition = Vector2.zero;
 private float scrollVelocity = 0;
 private float timeTouchPhaseEnded;
 private float inertiaDuration = 0.5f;
  
 private float itemInertiaDuration = 1.0f;
 private float itemTimeTouchPhaseEnded;
 private float rotateVelocityX = 0;
 private float rotateVelocityY = 0;
  
  
 public RaycastHit hit;
 
 //notsure about this one, but no time 
 
 private var layerMask = (1 <<  8) | (1 << 2);
Comment
Add comment · Show 1 · 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 NicoLocke · Dec 10, 2013 at 04:22 PM 1
Share

Goanna do this myself, wanted someone just to do it quickly but I guess i'm not goanna learn otherwise ^_^

Trying to do this at work too XD

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image

Related Questions

Convert this line of javascript to C# (easy) 1 Answer

converting javascript to c# 1 Answer

Trouble converting player controller from JS to C# 1 Answer

Script Convert 1 Answer

Trouble with converting some JS code to C# 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