C# movement code with the maximum movement speed.
Looks simple, but I can't get it done for hours!Need help!
Comment
Answer by Zoogyburger · Feb 22, 2016 at 11:16 PM
What kind of Player Movement are you looking for? Here's an example:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Rigidbody2D m_Rigidbody;
public float m_Speed ;
public float m_JumpHeight ;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {
// Use this for initialization
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
if (Input.GetKeyDown (KeyCode.Space)&& grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
}
if (Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
doubleJumped = true;
}
if (Input.GetKey (KeyCode.D)) {
m_Rigidbody.velocity = new Vector2 (m_Speed, m_Rigidbody.velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
m_Rigidbody.velocity = new Vector2 (-m_Speed, m_Rigidbody.velocity.y);
}
}
}