0
\$\begingroup\$

I am making my very first GAME in Unity 2d. At the moment my game consists of 2 walls a floor, a ceiling, and a square which represents the character. After watching some tutorials I have created a C# character controller script which at the moment let's my player jump. Now that I can make the square jump, my next goal is to make the character move without having to press any buttons or making it think a button is being pressed.

Question:

How do I make my square move to the right automatically without any input from the user?

Current Code:

usingUnityEngine;  
usingSystem.Collections;

public class SquareControllerScript : MonoBehaviour  
{  
    boolgrounded = false;  
    publicTransformgroundCheck;  
    floatgroundRadius = 0.2f;  
    publicLayerMaskwhatIsGround;  
    publicfloatjumpForce = 700f;  

    void FixedUpdate()  
    {  
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);  
    }  

    void Update()  
    {  
        if (grounded && Input.GetKeyDown (KeyCode.Space))   
        {  
            rigidbody2D.AddForce(newVector2(0,jumpForce));  
        }  
    }  
}  
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

As you may have noticed, the Update function is executed every frame as long as the MonoBehaviour is enabled. You can assign a constant velocity to the Rigidbody of the square instance in the Update function, so it moves at a constant velocity every frame.

if (grounded) {
    if (Input.GetKeyDown(KeyCode.Space)) {
        rigidbody2D.AddForce(new Vector2(0, jumpForce));
    } else {
        rigidbody2D.velocity = new Vector2(2, 0);
    }
}

UPDATE

Savlon's comment,

  • Use FixedUpdate when dealing with Rigidbody.
\$\endgroup\$
5
  • \$\begingroup\$ Thanks Aldour. I just tried "myTransform.position = Vector2.right * Time.time;" which did work in getting the character object to move to the right automatically, but now it isn't affected by the gravity, so I can't jump. I will try your method now. \$\endgroup\$
    – user49628
    Jul 24, 2014 at 3:55
  • \$\begingroup\$ You're welcome. By the way, you may want to experiment with modifying other propeties of a Rigidbody as well, such as acceleration and mass. Though these properties are not directly related to your question, these are useful physics-related stuff which you may find useful later on. \$\endgroup\$
    – S.C.
    Jul 24, 2014 at 4:03
  • \$\begingroup\$ I will definitely keep all of that in mind, before I start working on my next game. \$\endgroup\$
    – user49628
    Jul 24, 2014 at 5:45
  • \$\begingroup\$ My square is now able to jump and move right, but having trouble getting it to bounce off the walls at the moment I am using this 'if(collider.tag == "Left Wall") {rigidbody2D.velocity = new Vector2(2,0); }' \$\endgroup\$
    – user49628
    Jul 24, 2014 at 5:52
  • \$\begingroup\$ This quote is taken from docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html… FYI "FixedUpdate should be used instead of Update when dealing with Rigidbody." \$\endgroup\$
    – Savlon
    Jul 24, 2014 at 7:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .