C# to Javascript, arrgghh!!

Hi guys,

im only new to unity and am struggling to convert a C# document to Javascript, ive tried a bunch of things but i cant seem to get it to behave the way it does in C#

Can anyone help out to convert this to Javascript?

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{

	public float speed;
	
	void FixedUpdate ()
	{
		float  moveHorizontal = Input.GetAxis("Horizontal");
		float  moveVertical = Input.GetAxis("Vertical");
		
		
		
		Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
		rigidbody.AddForce(movement * speed * Time.deltaTime);
	}
	
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Pickup")
		{
			other.gameObject.SetActive(false);
		}

	}
}

much appreciated :slight_smile:

How does your converted js script looks so far?

Hi Mgear,

ive been messing around with a whole bunch of stuff which ive deleted since but this is what i have so far but it is very ridged in its movement, im guessing its because of the Vector3 but i just dont know how to integrate it yet :frowning:

this is what i have so far

var charSpeed = 8;
 
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * charSpeed ;
var y = Input.GetAxis("Vertical") * Time.deltaTime * charSpeed ;

transform.Translate(x, 0, y);
}

as for the rest, im stuck

cheers

Change the types to var (e.g., “float moveHorizontal” to “var moveHorizontal”) and void to function. Also, not that it’s related to languages, but change other.gameObject.tag == “Pickup” to other.CompareTag(“Pickup”).

–Eric

//the UnityScript format goes -> var varibleName : typeOfVarible; or just -> var varibleName;
var speed : float;

    
    //methods use the "function" keyword in UnityScript
    function FixedUpdate ()


    {


        var moveHorizontal : float = Input.GetAxis("Horizontal");


        var moveVertical  : float = Input.GetAxis("Vertical");


        


        


        
         //the "new" keyword is not need in UnityScript but is good practice to code explicitly 
        var movement  : Vector3 = new Vector3(moveHorizontal,0.0f,moveVertical);


        rigidbody.AddForce(movement * speed * Time.deltaTime);


    }


    


    


    function OnTriggerEnter(Collider other)


    {
        //not a needed edit but Eric suggested it(I really have no idea the benefits of this method)
        if(other.gameObject.CompareTag("Pickup"))


        {


            other.gameObject.SetActive(false);


        }


 


    }

guys! i cant thank you enough!! i tweeked it a little as the trigger function returned errors but its all working now.

Im pretty gump at this stuff but im learning.

thanks guys!

:smile: