Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerMove : MonoBehaviour
- {
- public float horSpeed = 10;
- public float verticaImpulse;
- static float speedX;
- public Transform child;
- bool isGrounded;
- bool useShot;
- new private Rigidbody2D rigidbody;
- private Animator animator;
- private Snowball snowball;
- private void Awake()
- {
- rigidbody = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- snowball = Resources.Load<Snowball>("Snowball");
- }
- // Перемещенние и прыжок
- public void LeftButtonDown()
- {
- speedX = -horSpeed;
- child.transform.rotation = Quaternion.Euler(0, 180, 0);
- State = PlayerState.movehor;
- useShot = false;
- }
- public void RightButtonDown()
- {
- speedX = horSpeed;
- child.transform.rotation = Quaternion.Euler(0, 0, 0);
- State = PlayerState.movehor;
- useShot = false;
- }
- public void StopMove()
- {
- speedX = 0;
- State = PlayerState.idle;
- }
- public void OnClickJumb()
- {
- if(isGrounded)
- rigidbody.AddForce(new Vector2(0, verticaImpulse * 100 * Time.deltaTime), ForceMode2D.Impulse);
- }
- public void OnCLickShoot()
- {
- useShot = true;
- Invoke("ShotSnowball", 0.417f);
- }
- public void ShotSnowball()
- {
- Vector3 position = transform.position;
- position.z = 10.0f;
- position.y = -0.17f;
- Snowball newSnowball = Instantiate(snowball, position, snowball.transform.rotation) as Snowball;
- if (child.eulerAngles.y == 180)
- {
- newSnowball.Direction = newSnowball.transform.right * -1.0f;
- }
- if (child.eulerAngles.y == 0)
- {
- newSnowball.Direction = newSnowball.transform.right * 1.0f;
- }
- }
- void FixedUpdate()
- {
- transform.Translate(speedX * Time.deltaTime, 0, 0);
- }
- void Update()
- {
- animator.SetBool("Shot", useShot);
- useShot = false;
- }
- // Проверка на соприкосновения с землей
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- isGrounded = true;
- }
- }
- private void OnCollisionExit2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- isGrounded = false;
- }
- }
- private PlayerState State
- {
- get { return (PlayerState)animator.GetInteger("State"); }
- set { animator.SetInteger("State", (int)value); }
- }
- }
- public enum PlayerState
- {
- idle,
- movehor
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement