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);
}
}
}
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
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);
}
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”).
//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);
}
}