用Unity的简易CS制作(不完整版)

用Unity的简易CS制作(不完整版)

学会了AI和UI的一部分操作和使用,但是对animator的一些代码操作还学深入学习,对于大型项目要学会分好不同的类,并用GitHub管理。

***Palyer类***
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Player : MonoBehaviour
{
    public Transform m_transform;
    public Transform cameraTransform;
    float m_cameraHeight = 1.4f;
    Vector3 m_camRotation;
    CharacterController m_ch;
    float m_moveSpeed = 5.0f;
    float m_gravity = 6.0f;
    public float m_jumpSpeed = 8.0f;
    public int m_life = 5;
    private Vector3 m_moveDirection = Vector3.zero;
    Transform m_muzzlepoint;
    public LayerMask m_layer;
    public Transform m_fx;
    public AudioClip m_audio;
    float m_shootTimer = 0;

    void Start()
    {
        m_transform = this.transform;
        m_ch = this.GetComponent<CharacterController>();
        cameraTransform = Camera.main.transform;
        Vector3 pos = m_transform.position;
        pos.y += m_cameraHeight;
        cameraTransform.position = pos;
        cameraTransform.rotation = m_transform.rotation;
        m_camRotation = cameraTransform.eulerAngles;
        Cursor.lockState = CursorLockMode.Locked;
        m_muzzlepoint = cameraTransform.Find("M16/weapon/muzzlepoint").transform;
    }
    void Update()
    {
        if (m_life <= 0)
        {
            return;
        }
        Control();

        m_shootTimer -= Time.deltaTime;
        if (Input.GetMouseButton(0) && m_shootTimer <= 0)
        {
            m_shootTimer = 0.1F;
            GetComponent<AudioSource>().PlayOneShot(m_audio);
            GameMananger.instance.SetAmmo(1);
            RaycastHit info;
            bool hit = Physics.Raycast(m_muzzlepoint.position, cameraTransform.TransformDirection(Vector3.forward), out info, 100, m_layer);
            if (hit)
            {
                if (info.transform.tag.CompareTo("enenmy") == 0)
                {
                    Enemy enemy = info.transform.GetComponent<Enemy>();
                    enemy.OnDamage(1);
                }
                Instantiate(m_fx, info.point, info.transform.rotation);
            }
        }


    }
    void Control()
        {
            float rh = Input.GetAxis("Mouse X");
            float rv = Input.GetAxis("Mouse Y");
            m_camRotation.x -= rv;
            m_camRotation.y += rh;
            cameraTransform.eulerAngles = m_camRotation;
            Vector3 camRot = cameraTransform.eulerAngles;
            camRot.x = 0;
            camRot.z = 0;
            m_transform.eulerAngles = camRot;
            Vector3 pos = m_transform.position;
            pos.y += m_cameraHeight;
            cameraTransform.position = pos;

            float xm = 0, ym = 0, zm = 0;
            ym -= m_gravity * Time.deltaTime;
            if (Input.GetKey(KeyCode.W))
            {
                zm += m_moveSpeed * Time.deltaTime;
            }

            else if (Input.GetKey(KeyCode.S))
            {
                zm -= m_moveSpeed * Time.deltaTime;
            }

            else if (Input.GetKey(KeyCode.A))
            {
                xm -= m_moveSpeed * Time.deltaTime;
            }

            else if (Input.GetKey(KeyCode.D))
            {
                xm += m_moveSpeed * Time.deltaTime;
            }

            else if (Input.GetKeyDown(KeyCode.Space))
            {
                m_moveDirection.y = m_jumpSpeed;
            }

            m_moveDirection.y -= m_gravity * Time.deltaTime;

            m_ch.Move(m_transform.TransformDirection(new Vector3(xm, ym, zm)));
            m_ch.Move(m_moveDirection * Time.deltaTime);
        }
    public void OnDamage(int damage)
        {
            m_life -= damage;
            GameMananger.instance.SetLife(m_life);
            if (m_life <= 0)
            {
                Cursor.lockState = CursorLockMode.None;
            }
        }
    
}



***Enenmy类***

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

public class Enemy : MonoBehaviour
{
    Transform m_transform;
    Player m_player;
    NavMeshAgent m_agent;
    Animator m_ani;
    float m_moveSpeed = 2.5f;
    float m_rotSpeed = 30;
    float m_timer = 2;
    int m_life = 15;
    void Start()
    {
        m_transform = this.transform;
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        m_agent =this. GetComponent<NavMeshAgent>();
        m_ani =this. GetComponent<Animator>();
        m_agent.speed = m_moveSpeed;
        /* m_agent.SetDestination(m_player.m_transform.position);*/
        Ray ray = Camera.main.ScreenPointToRay(m_player.m_transform.position);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            print(hit.point);
        }
        m_agent.SetDestination(hit.point);


    }

    void RotateTo()
    {
        Vector3 targetdir = m_player.m_transform.position - m_transform.position;
        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetdir, m_rotSpeed * Time.deltaTime, 0.0f);
        m_transform.rotation = Quaternion.LookRotation(newDir);
    }

    private void Update()
    {
        if (m_player.m_life <= 0)
            return;
        AnimatorStateInfo stateInfo = m_ani.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.idle") && !m_ani.IsInTransition(0))
        {
            m_ani.SetBool("idle", false);
            m_timer -= Time.deltaTime;
            if (m_timer > 0) return;
            if (Vector3.Distance(m_transform.position, m_player.m_transform.position) < 1.5f)
            {
                m_ani.SetBool("attack", true);
            }
            else
            {
                m_timer = 1;
                m_agent.SetDestination(m_player.m_transform.position);
                m_ani.SetBool("run", true);

            }
            if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.run") && !m_ani.IsInTransition(0))
            {
                m_ani.SetBool("run", false);
                m_timer -= Time.deltaTime;
                if (m_timer < 0)
                {
                    m_agent.SetDestination(m_player.m_transform.position);
                    m_timer = 1;
                }
                if (Vector3.Distance(m_transform.position, m_player.m_transform.position) <= 1.5f)
                {
                    m_ani.enabled = false;
                    m_ani.SetBool("attack", true);
                }
            }

            if (stateInfo.fullPathHash  ==  Animator.StringToHash("Base Layer.attack") && !m_ani.IsInTransition(0))
            {
                RotateTo();
                m_ani.SetBool("attack", false);
                if (stateInfo.normalizedTime >= 1.0f)
                {
                    m_ani.SetBool("idle", true);
                    m_timer = 2;
                    m_player.OnDamage(1);
                }
            }
            if(stateInfo.fullPathHash==Animator.StringToHash("Base Layer.death")&&!m_ani.IsInTransition(0))
            {
                if(stateInfo.normalizedTime>=1.0f)
                {
                    GameMananger.instance.SetScore(100);
                    Destroy(this.gameObject);
                }
            }
        }
    }

    public void OnDamage(int damage)
    {
        m_life -= damage;
        if(m_life<=0)
        {
            m_ani.SetBool("death", true);
        }
    }
}


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值