0

I'm attempting to create a game in unity, but Java isn't able to be used in it, so any premade scripts are in C#. I want to add some stuff in the mechanics of the game, which requires me to alter variables and values in the script, but I only know how to do so in java, so how would I make it so they can effectively communicate?

An example from c#:

    protected override void ComputeVelocity()
{
    Vector2 move = Vector2.zero;

    move.x = Input.GetAxis ("Horizontal");
    if (Input.GetButtonDown ("Jump") && grounded) {
        velocity.y = jumpTakeOffSpeed;
    } else if (Input.GetButtonUp ("Jump"))
    {
        if (velocity.y > 0)
            velocity.y = velocity.y * .5f;
    }

    targetVelocity = move * maxSpeed;

}
}

and my java code:

public void keyPressed(KeyEvent e) 
{
    if(e.getKeyCode() == KeyEvent.VK_SHIFT)
    { 
        endTime = (System.currentTimeMillis() / 1000);
        timePassed = endTime - startTime; 
        if(timePassed >= 2)
        {   

            //try to set a time limit or something 

            velocity = overMaxVelocity;
            //set velocity to above usual max for dodgeTime 
            startTime = dodgeTime + (System.currentTimeMillis() / 1000);
        }


    }

}

I'm trying to make it so when shift is pressed, the velocity is changed to a larger value than usual for a small time, but i have no idea where to even begin

2
  • Java is close enough that most of this should make sense.. Sadly you need to do it in c# Commented Mar 29, 2019 at 22:43
  • 2
    You'll need to learn C#, which I don't think you'll regret anyway. Commented Mar 29, 2019 at 22:46

1 Answer 1

2

Unity only supports scripts written in C#. It used to also support a version of JavaScript they called UnityScript, but they moved to only officially support C# now. Fortunately C# is really similar to Java, so you shouldn't have too much trouble translating your scripts to C#. The main challenge would be learning the Unity library.

I wrote some code below that updates an object's speed using Unity library functions. Unity has a lot of built-in ways of helping you out as a developer so I'd recommend the tutorials on Unity's website for more on getting started with it.

public float speed = 2;
public float speedUpFactor = 2;

// Get the Rigidbody component attached to this gameobject
// The rigidbody component is necessary for any object to use physics)
// This gameobject and any colliding gameobjects will also need collider components
Rigidbody rb;
// Start() gets called the first frame that this object is active (before Update)
public void Start(){
    // save a reference to the rigidbody on this object
    rb = GetComponent<Rigidbody>();
}
}// Update() gets called every frame, so you can check for input here.
public void Update() {

    // Input.GetAxis("...") uses input defined in the "Edit/Project Settings/Input" window in the Unity editor.
    // This will allow you to use the xbox 360 controllers by default, "wasd", and the arrow keys.
    // Input.GetAxis("...") returns a float between -1 and 1
    Vector3 moveForce = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveForce *= speed;

    // Input.GetKey() returns true while the specified key is held down
    // Input.GetKeyDown() returns true during the frame the key is pressed down
    // Input.GetKeyUp() returns true during the frame the key is released
    if(Input.GetKey(KeyCode.Shift)) 
    {
        moveForce *= speedUpFactor;
    }
    // apply the moveForce to the object
    rb.AddForce(moveForce);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.