C# Codes
For Character Movement (Move left and right)
- using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
public float maxSpeed = 6.0f;
public bool facingRight = true;
public float moveDirection;
public float jumpSpeed = 600.0f;
//Check Ground
public bool grounded = false;
public Transform groundCheck;
public float groundRadius = 2.0f;
public LayerMask whatIsGround;
void Awake()
{
groundCheck = GameObject.Find ("GroundCheck").transform;
}
// Use this for initialization
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
GetComponent<Rigidbody> ().velocity = new Vector2 (moveDirection * maxSpeed, GetComponent<Rigidbody> ().velocity.y);
if (moveDirection > 0.0f && !facingRight) {
Flip ();
} else if (moveDirection < 0.0f && facingRight) {
Flip ();
}
}
void Flip(){
facingRight = !facingRight;
transform.Rotate (Vector3.up, 180.0f, Space.World);
}
// Update is called once per frame
void Update () {
moveDirection = Input.GetAxis("Horizontal");
if (grounded && Input.GetButtonDown ("Jump")) {
GetComponent<Rigidbody> ().AddForce (new Vector2 (0, jumpSpeed));
}
}
}
For camera to follow the character movement.
- using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public GameObject Character;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - Character.transform.position;
}
// Update is called once per frame
void Update () {
transform.position = Character.transform.position + offset;
}
}
0 comments:
Post a Comment