20 กันยายน 2559

Unity : 2D Game with Unity Part # 5




// โค้ดคำสั่ง Main_Controller.cs

using UnityEngine;
using System.Collections;

public class Man_Controller : MonoBehaviour {
public float speed;
float x, sx;
bool ks;
Animator am;
Rigidbody2D rb;

// Use this for initialization
void Start () {
am = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
sx = transform.localScale.x;
}

// Update is called once per frame
void Update () {
x = Input.GetAxis ("Horizontal");
am.SetFloat ("speed", Abs (x));


if (Input.GetButtonDown ("Jump")) {
am.SetBool ("jump", true);
rb.velocity = new Vector2 (rb.velocity.x, 5f);
}

rb.velocity = new Vector2 (x * speed, rb.velocity.y);

if (x > 0) {
transform.localScale = new Vector3 (sx, transform.localScale.y, transform.localScale.z);
}

if (x < 0) {
transform.localScale = new Vector3 (-sx, transform.localScale.y, transform.localScale.z);
}
}

void OnCollisionEnter2D(Collision2D coll) {
am.SetBool ("jump", false);
}
float Abs(float x) {
return x >= 0f ? x : -x;
}
}



// โค้ดคำสั่ง CameraFollow.cs
using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {
public Transform obj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (obj.position.x, transform.position.y, transform.position.z);
}
}

ไม่มีความคิดเห็น:

"I Believe in You"

Copyright(c) 2007 - 2022 by Kasem Kamolchaipisit.