第四周博客

游戏角色二维刚体,移动,转身定义

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

public class player : MonoBehaviour {
    private Rigidbody2D myRig;
    private bool faceright;
    // Use this for initialization
    void Start () {
        myRig = GetComponent<Rigidbody2D>();
        faceright = true;
    }
    
    // Update is called once per frame
    void Update () {
        float horizonta = Input.GetAxis("Horizontal");
        moveMent(horizonta);
        flip(horizonta);
    }
    private void moveMent(float horizonta)
    {
        myRig.velocity = new Vector2(horizonta,myRig.velocity.y);
    }
    private void flip(float horizonta)
    {
        if(horizonta>0 && !faceright || horizonta<0 && faceright)
        {
            faceright = !faceright;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }
}

射击部分内容代码

using UnityEngine;

/// <summary>
/// Launch projectile
/// </summary>
public class weapon1 : MonoBehaviour
{
    //--------------------------------
    // 1 - Designer variables
    //--------------------------------

    /// <summary>
    /// Projectile prefab for shooting
    /// </summary>
    public Transform shotPrefab;

    /// <summary>
    /// Cooldown in seconds between two shots
    /// </summary>
    public float shootingRate = 0.25f;

    //--------------------------------
    // 2 - Cooldown
    //--------------------------------

    private float shootCooldown;

    void Start()
    {
        shootCooldown = 0f;
    }

    void Update()
    {
        if (shootCooldown > 0)
        {
            shootCooldown -= Time.deltaTime;
        }
    }

    //--------------------------------
    // 3 - Shooting from another script
    //--------------------------------

    /// <summary>
    /// Create a new projectile if possible
    /// </summary>
    public void Attack(bool isEnemy)
    {
        if (CanAttack)
        {
            shootCooldown = shootingRate;

            // Create a new shot
            var shotTransform = Instantiate(shotPrefab) as Transform;

            // Assign position
            shotTransform.position = transform.position;

            // The is enemy property
            code1 shot = shotTransform.gameObject.GetComponent<code1>();
            if (shot != null)
            {
                shot.isEnemyShot = isEnemy;
            }

            // Make the weapon shot always towards it
            player2 move = shotTransform.gameObject.GetComponent<player2>();
            if (move != null)
            {
                move.direction = transform.right; // towards in 2D space is the right of the sprite
            }
        }
    }

    /// <summary>
    /// Is the weapon ready to create a new projectile?
    /// </summary>
    public bool CanAttack
    {
        get
        {
            return shootCooldown <= 0f;
        }
    }
}

由于测试出现游戏角色自动后退和弹道无法跟随角色移动的bug,暂在修复当中,博客也是赶工出来的。所以分享的内容有限,望老师见谅。

猜你喜欢

转载自www.cnblogs.com/xonoc/p/9906441.html