# Automatic Movement

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;
publicfloatjumpForce = 700f;

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

void Update()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
}
}
}


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)) {
} else {
rigidbody2D.velocity = new Vector2(2, 0);
}
}


UPDATE

Savlon's comment,

• Use FixedUpdate when dealing with Rigidbody.
• 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. – user49628 Jul 24 '14 at 3:55
• 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. – S.C. Jul 24 '14 at 4:03
• I will definitely keep all of that in mind, before I start working on my next game. – user49628 Jul 24 '14 at 5:45
• 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); }' – user49628 Jul 24 '14 at 5:52
• This quote is taken from docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html… FYI "FixedUpdate should be used instead of Update when dealing with Rigidbody." – Savlon Jul 24 '14 at 7:02