Unity 2d player shooting.

Code

PlayerTankActions.cs

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class PlayerTankActions : MonoBehaviour
{
    [SerializeField] private float movementSpeed = 2, rotationSpeedTank = 180, rotationSpeedTurret = 220;

    private Transform tankBody, tankTurret, barrel;
    private Rigidbody2D _rigidbody2D;
    
    // Start is called before the first frame update
    void Start()
    {
        tankBody = transform.Find("Chassie");
        tankTurret = transform.Find("Turret");
        barrel = tankTurret.Find("Barrel");

        _rigidbody2D = GetComponent<Rigidbody2D>();
    }
    
    public void MovePlayer(float inputValue) 
        => _rigidbody2D.velocity = tankBody.right * movementSpeed * Mathf.Clamp01(inputValue);

    public void RotateTankBody(float inputValue)
    {
        float rotation = -inputValue * rotationSpeedTank;
        tankBody.Rotate(Vector3.forward * rotation * Time.deltaTime);
    }

    public void RotateTankTurret(Vector3 endPoint)
    {
        Quaternion desiredRotation = Quaternion.LookRotation(Vector3.forward, endPoint - tankTurret.position);
        desiredRotation = Quaternion.Euler(0, 0, desiredRotation.eulerAngles.z + 90);
        tankTurret.rotation = Quaternion.RotateTowards(tankTurret.rotation, desiredRotation, rotationSpeedTurret * Time.deltaTime);
    }

    public void Shoot(GameObject prefab)
    {
        GameObject newBullet = Instantiate(prefab, barrel.position, tankTurret.rotation);
    }
}

PlayerController.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private float _horizontalInput, _verticalInput;
    private Vector3 _direction;
    private bool _isShooting;
    
    private PlayerTankActions _actionScript;
    [SerializeField] private GameObject bullet;
    
    // Start is called before the first frame update
    void Start() => _actionScript = GetComponent<PlayerTankActions>();

    // Update is called once per frame
    void Update()
    {
        GetPlayerInput();
        _actionScript.RotateTankBody(_horizontalInput);
        _actionScript.RotateTankTurret(_direction);

        if (_isShooting)
        {
            Shoot();
            _isShooting = false;
        }
    }

    private void Shoot()
    {
        _actionScript.Shoot(bullet);
    }

    private void FixedUpdate()
    {
        _actionScript.MovePlayer(_verticalInput);
    }

    private void GetPlayerInput()
    {
        _horizontalInput = Input.GetAxisRaw("Horizontal");
        _verticalInput = Input.GetAxisRaw("Vertical");
        _direction = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,
            Camera.main.nearClipPlane));

        _isShooting = Input.GetKeyDown(KeyCode.Space);
    }
}

Bullet.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    [SerializeField] private float speed = 8.0f, selfDestructionTime = 1.2f;

    private Rigidbody2D _rigidbody2D;
    
    // Start is called before the first frame update
    void Start()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();
        _rigidbody2D.velocity = transform.right * speed;
        Destroy(gameObject, selfDestructionTime);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("obstacle"))
        {
            Destroy(gameObject);
        }
    }
}

posted @ 2022-12-29 16:16  yassine  阅读(231)  评论(0编辑  收藏  举报