Hey guys, just a quick question, I need to translate the following script for line creation, it’s written in Java Script but I’m much more fluent in C#, could someone help me out?
I tried the online conversion system but obviously it can’t handle var’s.
#pragma strict
var pos1 : Vector3;
var pos2 : Vector3;
var objectHeight = 2.0; // 2.0 for a cylinder, 1.0 for a cube
function Update () {
if (Input.GetMouseButtonDown(0)) {
pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0)) {
pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = v3.magnitude/objectHeight;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
}
Thanks in advanced guys!
Okay … here you go …
using UnityEngine;
using System.Collections;
public class SampleClass: MonoBehaviour
{
Vector3 pos1;
Vector3 pos2;
float objectHeight= 2.0f; // 2.0f for a cylinder, 1.0f for a cube
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0))
{
pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1)
{
Vector3 v3= pos2 - pos1;
transform.position = pos1 + (v3) / 2.0f;
transform.localScale = new Vector3(transform.localScale.x, v3.magnitude/objectHeight, transform.localScale.z);
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
}
[RPC]
void Test ()
{}
}
Use Convert unity javascript (unityscript) to C# like sriram suggested and just replace FIXME_VAR_TYPE with the keyword var .
So this:
FIXME_VAR_TYPE objectHeight= 2.0f;
Can become
var objectHeight= 2.0f;
or
float objectHeight= 2.0f;
Both is correct C# code. I used the converter many times, without problems.