Добрый день, прошу помочь. Есть скрипт на персонажа который двигается горизонтально, как сделать чтобы персонаж двигался во все стороны влево, вправо, вверх и вниз, и при этом видел стены?
Код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Character : Unit
{
[SerializeField]
private int lives = 6;
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private float jumpForce = 15.0F;
private bool isGrounded = false;
public float spawnX, spawnY;
private Bullet bullet;
private Charstate State
{
get
{
return (Charstate)animator.GetInteger("State");
}
set
{
animator.SetInteger("State", (int)value);
}
}
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
bullet = Resources.Load<Bullet>("Bullet");
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
if (isGrounded) State = Charstate.Idle;
if (Input.GetButtonDown("Fire1")) Shoot();
if (Input.GetButton("Horizontal")) Run();
if (isGrounded && Input.GetButtonDown("Jump")) Jump();
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
if (isGrounded) State = Charstate.Run;
}
private void Jump()
{
State = Charstate.Jump;
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot()
{
Vector3 position = transform.position; position.y += 0.7F;
if (sprite.flipX)
position.x -= 0.5F;
else
position.x += 0.5F;
Bullet newBullet = Instantiate(bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * (sprite.flipX ? -1.0F : 1.0F);
}
public override void ReceiveDamage()
{
lives--;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 8.0F, ForceMode2D.Impulse);
if(lives < 1) SceneManager.LoadScene("Death");
Debug.Log(lives);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
if (!isGrounded) State = Charstate.Jump;
}
private void OnCollisionEnter2D(Collision2D collider)
{
if (collider.gameObject.name == "Portal")
SceneManager.LoadScene("Ekran");
}
private void OnTriggerEnter2D(Collider2D collider)
{
Unit unit = collider.gameObject.GetComponent<Unit>();
if (unit)
{
Debug.Log("OK");
ReceiveDamage();
}
}
public enum Charstate
{
Idle,
Run,
Jump,
Shoot
}
}