- Home /
Unity 2D - Make Player Auto-Move?
Hello, I'm making a 2D sidescroller game in which I want the player to automatically move to the right, HOW DO I DO THIS?!? I've looked at tutorials and stuff but I cant find an answer!
heres my script
{ using UnityEngine; using System.Collections;
public class CharacterControllerScript : MonoBehaviour { public float maxSpeed = 10f; bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D> ().velocity.y);
//float move = Input.GetAxis ("Horizontal");
//anim.SetFloat ("Speed", Mathf.Abs (move));
//GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
//if (move > 0 && !facingRight)
//Flip ();
//else if (move < 0 && facingRight)
//Flip ();
}
void Update()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D> ().AddForce(new Vector2(0, jumpForce));
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
} }
Answer by KoJix · May 19, 2015 at 12:43 PM
What if you change
//float move = Input.GetAxis ("Horizontal"); //anim.SetFloat ("Speed", Mathf.Abs (move)); //GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
To
float move = 1.0f;
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
Your answer
Follow this Question
Related Questions
How can I add a acharacter to a 2d game? 1 Answer
2d Character movement script (unity 4,3) 2 Answers
cant jump in 2d? 4 Answers