[Thảo Luận] Giúp Script Di chuyển 2D :(
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float moveForce = 20f;
public float jumpForce = 700f;
public float maxVelocity = 4f;
private bool grounded;
private Rigidbody2D myBody;
private Animator anim;
void Awake(){
myBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator> ();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
PlayerWalkKeyBoard ();
}
void PlayerWalkKeyBoard (){
float forceX = 0f;
float forceY = 0f;
float vel = Mathf.Abs (myBody.velocity.x);
float h = Input.GetAxisRaw("Horizontal");//-1 0 1
if (h > 0) {
if (vel < maxVelocity) {
if (grounded) {
forceX = moveForce;
}else{
forceX = moveForce * 1.1f;
}
}
Vector3 scale = transform.localScale;
scale.x = 1f;
transform.localScale = scale;
anim.SetBool("Walk", true);
}else
if (h < 0) {
if (vel < maxVelocity) {
if (grounded) {
forceX = -moveForce;
}else{
forceX = -moveForce * 1.1f;
}
}
Vector3 scale = transform.localScale;
scale.x = -1f;
transform.localScale = scale;
anim.SetBool("Walk", true);
}else if (h == 0) {
anim.SetBool("Walk", false);
}
if (Input.GetKey (KeyCode.Space)) {
if (grounded) {
grounded = false;
forceY = jumpForce;
}
}
myBody.AddForce (new Vector2 (forceX, forceY) );
}
void OnCollisionEnter2D(Collision2D target) {
if (target.gameObject.tag == "Ground") {
grounded = true;
}
}
}
_______________________________________
Comments
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
anh rành unity,giúp em cái
Anh đang giờ làm việc, chưa thể xem code được, để tối Pirate giúp em nè
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
* Apply Force sẽ apply force
* Code của bạn có vấn đề là vì, trong quá trình bay, đôi khi vật sẽ va chạm với Ground khác, cho phép bạn apply force cho 1 vật đã có velocity rồi, dẫn đến bay quá cao.
* Ngoài ra, grounded không chính xác. Trong quá trình bay, đôi khi sẽ va chạm vật khác, như trên, thì không nhảy được, trừ khi bạn cố tình làm vậy.
Mình đang sửa lại.
https://drive.google.com/open?id=0B147z-w7sqHETlVpMnlIbTFEeVE
chuyển qua script khác rui bạn