[SOLVED] Help Convert this JS script to C#?

#pragma strict

var speed:float=5; //speed of the car, tweek as needed based on your scale
var turnSpeed:float=180; //turn speed

function Update()
{
    //grab the input axes
    var steer=Input.GetAxis("Horizontal");
    var gas=Input.GetAxis("Vertical");

//if they're hittin' the gas...
if (gas!=0)
{
     //take the throttle level (with keyboard, generally +1 if up, -1 if down)
     //  and multiply by speed and the timestep to get the distance moved this frame
     var moveDist=gas*speed*Time.deltaTime;

     //now the turn amount, similar drill, just turnSpeed instead of speed
     //   we multiply in gas as well, which properly reverses the steering when going
     //   backwards, and scales the turn amount with the speed
     var turnAngle=steer * turnSpeed * Time.deltaTime * gas;

     //now apply 'em, starting with the turn
     transform.rotation.eulerAngles.y+=turnAngle;

     //and now move forward by moveVect
     transform.Translate(Vector3.forward*moveDist);
}


}

Thanks in advance!

Make sure to change SomeName to whatever you want. But the name of the C# script file must also be named the same thing. Example if you keep SomeName as the name of your class the file must be called SomeName in Unity Editor

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SomeName : MonoBehaviour
{

    float speed =5; //speed of the car, tweek as needed based on your scale
    float turnSpeed =180; //turn speed
    private void Update()
    {
        //grab the input axes
        var steer = Input.GetAxis("Horizontal");
        var gas = Input.GetAxis("Vertical");

        //if they're hittin' the gas...
        if (gas != 0)
        {
            //take the throttle level (with keyboard, generally +1 if up, -1 if down)
            //  and multiply by speed and the timestep to get the distance moved this frame
            var moveDist = gas * speed * Time.deltaTime;

            //now the turn amount, similar drill, just turnSpeed instead of speed
            //   we multiply in gas as well, which properly reverses the steering when going
            //   backwards, and scales the turn amount with the speed
            var turnAngle = steer * turnSpeed * Time.deltaTime * gas;

            //now apply 'em, starting with the turn
            Vector3 eulerAngles = transform.rotation.eulerAngles;
            eulerAngles.y += turnAngle;
            transform.rotation = Quaternion.Euler(eulerAngles);

            //and now move forward by moveVect
            transform.Translate(Vector3.forward * moveDist);
        }
    }
}
1 Like

T

Thanks works great :slight_smile: