Bài tập thực hành: Play control môn Cơ sở lập trình cuối học kì 2 (p1) | Đại học Văn Lang

Bài tập thực hành: Play control môn Cơ sở lập trình cuối học kì 2 (p1) | Đại học Văn Lang giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học

Môn:
Trường:

Đại học Văn Lang 742 tài liệu

Thông tin:
2 trang 9 tháng trước

Bình luận

Vui lòng đăng nhập hoặc đăng ký để gửi bình luận.

Bài tập thực hành: Play control môn Cơ sở lập trình cuối học kì 2 (p1) | Đại học Văn Lang

Bài tập thực hành: Play control môn Cơ sở lập trình cuối học kì 2 (p1) | Đại học Văn Lang giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học

103 52 lượt tải Tải xuống
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
// Start is called before the first frame update
moveForce = 20f;public float
jumpForce = 300f;public float
maxVelocity = 4f;public float
isGrounded;private bool
_score = 0;public int
Rigidbody2D myBody;private
Animator animator;private
Start()void
{
}
Awake()void
{
myBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
Update()void
{
// if (Static.s_score >= 10)
{
// Application.LoadLevel("Scene2");
}
}
// Update is called once per frame
FixedUpdate()void
{
PlayerWalk();
}
PlayerWalk()void
{
forceX = 0f;float
forceY = 0f;float
vel = Mathf.Abs(myBody.velocity.x);float
h = Input.GetAxisRaw(float "Horizontal");
(h > 0)if
{
(vel < maxVelocity)if
{
forceX = moveForce;
}
Vector3 scale = transform.localScale;
scale.x = 2f;
transform.localScale = scale;
animator.SetInteger( , 1);"state"
}
(h < 0)else if
{
(vel < maxVelocity)if
{
forceX = -moveForce;
}
Vector3 scale = transform.localScale;
scale.x = -2f;
transform.localScale = scale;
animator.SetInteger( , 1);"state"
}
(h == 0)else if
{
animator.SetInteger( , 0);"state"
}
(Input.GetKey(KeyCode.UpArrow))if
{
(isGrounded)if
{
isGrounded = ;false
forceY = jumpForce;
animator.SetInteger( , 2);"state"
}
}
(Input.GetKey(KeyCode.F))if
{
animator.SetInteger( , 4);"state"
}
(Input.GetKey(KeyCode.G))if
{
animator.SetInteger( , 3);"state"
}
(Input.GetKey(KeyCode.H))if
{
animator.SetInteger( , 5);"state"
}
myBody.AddForce( Vector2(forceX, forceY));new
}
OnCollisionEnter2D(Collision2D target)void
{
(target.gameObject.tag == )if "Ground"
{
isGrounded = ;true
}
}
}
| 1/2

Preview text:

using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class PlayerControl : MonoBehaviour {
// Start is called before the first frame update public float moveForce = 20f;
public float jumpForce = 300f;
public float maxVelocity = 4f; private bool isGrounded; public int _score = 0; private Rigidbody2D myBody; private Animator animator; Start() void { } Awake() void { myBody = GetComponent(); animator = GetComponent(); } Update() void {
// if (Static.s_score >= 10) {
// Application.LoadLevel("Scene2"); } }
// Update is called once per frame FixedUpdate() void { PlayerWalk(); } PlayerWalk() void { float forceX = 0f; float forceY = 0f;
float vel = Mathf.Abs(myBody.velocity.x);
float h = Input.GetAxisRaw("Horizontal"); if (h > 0) { if (vel < maxVelocity) { forceX = moveForce; }
Vector3 scale = transform.localScale; scale.x = 2f; transform.localScale = scale;
animator.SetInteger("state", 1); } else if (h < 0) { if (vel < maxVelocity) { forceX = -moveForce; }
Vector3 scale = transform.localScale; scale.x = -2f; transform.localScale = scale;
animator.SetInteger("state", 1); } else if (h == 0) {
animator.SetInteger("state", 0); }
if (Input.GetKey(KeyCode.UpArrow)) { if (isGrounded) { isGrounded = false; forceY = jumpForce;
animator.SetInteger("state", 2); } } if (Input.GetKey(KeyCode.F)) {
animator.SetInteger("state", 4); } if (Input.GetKey(KeyCode.G)) {
animator.SetInteger("state", 3); } if (Input.GetKey(KeyCode.H)) {
animator.SetInteger("state", 5); }
myBody.AddForce(new Vector2(forceX, forceY)); } OnCollisionEnter2D(Collision2 void D target) { if (target.gameObject.tag == ) "Ground" { isGrounded = true; } } }