- Home /
Too subjective and argumentative
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.
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.
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 :\
well... i +1 you because it seems to be useful code for me and you also did the translation for me! :) thanks....
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.
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.
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);
}
}
}
}
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
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.
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);
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
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