Translate between C# and javascript
Hi, I have a movement script and a camera script that I've been adding onto and customizing to fit my needs (both are Javascript) and I'm getting pretty good at Javascript but once I look at C# I get all confused, I was wondering if anyone has any info on how to convert between the two.
This is the C# script (from import package) I want to convert to Javascript:
using UnityEngine; using System.Collections;
public class SmoothFollow : MonoBehaviour {
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
// Place the script in the Camera-Control group in the component menu
[AddComponentMenu("Camera-Control/Smooth Follow")]
void LateUpdate()
{
// Early out if we don't have a target
if (!target) return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
Answer by Jason2014 · Jun 17, 2017 at 01:30 PM
#pragma strict
public var target : Transform;
public var distance : float = 10;
public var height : float = 5;
public var heightDamping : float = 2;
public var rotationDamping : float = 3;
@script AddComponentMenu ("Camera-Control/Smooth Follow")
function LateUpdate ()
{
if (!target)
{
return;
}
var wantedRotationAngle : float = target.eulerAngles.y;
var wantedHeight : float = target.position.y + height;
var currentRotationAngle : float = transform.eulerAngles.y;
var currentHeight : float = transform.position.y;
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
var currentRotation : Quaternion = Quaternion.Euler (0, currentRotationAngle, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.position = Vector3 (transform.position.x, currentHeight, transform.position.z);
transform.LookAt (target);
}
It should work. I removed comments to display less messy code. Compare these two scripts and find differences in syntax.
EDIT: Also look on this video: https://www.youtube.com/watch?v=GPpw_ZE1LVc
Thank you SO $$anonymous$$UCH! Works perfectly! I've watched that video like 60 times :( but my JS version didn't work, it kept glitching lower on the y-axis.... I just compared them and I was missing two brackets on either side of return and the words "#pragma strict"... also I have messy formatting. You do clean work :)
Good to know I was close though! Thanks again, VERY much appreciated!
Don't mention it. Years of experience $$anonymous$$ched me how to write correct...and clean.
Your answer
Follow this Question
Related Questions
Convert Js yo C# 1 Answer
Rotate camera when colliding 0 Answers
Model TextureBlurring Effect (Optimization) 0 Answers