Skip to main content
added 151 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

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;

publicclassSquareControllerScript : MonoBehaviour
{
boolgrounded = false;
publicTransformgroundCheck;
floatgroundRadius = 0.2f;
publicLayerMaskwhatIsGround;
publicfloatjumpForce = 700f;

voidStart ()
{

}

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

voidUpdate()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
rigidbody2D.AddForce(newVector2(0,jumpForce));
}
}
}

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));  
        }  
    }  
}  

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;

publicclassSquareControllerScript : MonoBehaviour
{
boolgrounded = false;
publicTransformgroundCheck;
floatgroundRadius = 0.2f;
publicLayerMaskwhatIsGround;
publicfloatjumpForce = 700f;

voidStart ()
{

}

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

voidUpdate()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
rigidbody2D.AddForce(newVector2(0,jumpForce));
}
}
}

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));  
        }  
    }  
}  
Source Link

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;

publicclassSquareControllerScript : MonoBehaviour
{
boolgrounded = false;
publicTransformgroundCheck;
floatgroundRadius = 0.2f;
publicLayerMaskwhatIsGround;
publicfloatjumpForce = 700f;

voidStart ()
{

}

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

voidUpdate()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
rigidbody2D.AddForce(newVector2(0,jumpForce));
}
}
}