项目二笔记-噩梦射手

笔记:

创建Animation文件夹,创建Animation Controller,用状态机来设置Animator动画,两个bool量move和dead判断播放哪种动画。

控制移动的方法

        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        Vector3 move = new Vector3(h, 0, v);

        GetComponent<Rigidbody>().MovePosition(transform.position + move * speed * Time.deltaTime);

(之后继续补充)

用相机跟随时,用update会抖动。一般运动产生抖动情况时,用fix update方法来代替update。

getcomponent和getcomponentinchdren

前者是从该游戏物体中取得组件,后者是从该游戏物体的子物体中取得组件(之后补充)

gameObject和GameObject的区别:


Gameobject是个类,概念就像“人”;gameobject是个对象,是类的实例化,例如“张三”.

类名一般首字母大写!

补充:

1GetCompoment <T>()从当前游戏对象获取组件T,只在当前游戏对象中获取,没得到的就返回null,不会去子物体中去寻找。

2GetCompomentInChildren<T>()先从本对象中找,有就返回,没就子物体中找,知道找完为止。

3GetComponents<T>()获取本游戏对象的所有T组件,不会去子物体中找。

4GetComponentsInChildren<T>()=GetComponentsInChildren<T>(true)取本游戏对象及子物体的所有组件

如果子物体太多可以用public引用。


 MonoBehaviour.Update 更新

        MonoBehaviour启用时,其Update在每一帧被调用。


 MonoBehaviour.FixedUpdate 固定更新

        当MonoBehaviour启用时,其 FixedUpdate在每一帧被调用。

        处理Rigidbody时,需要用FixedUpdate代替Update。例如:给刚体加一个作用力时,你必须应用作用力在FixedUpdate里的固定帧,而不是Update中的帧。(两者帧长不同)


  MonoBehaviour.LateUpdate 晚于更新         当Behaviour启用时,其LateUpdate在每一帧被调用。
        LateUpdate是在所有Update函数调用后被调用。这可用于调整脚本执行顺序。例如:当物体在Update里移动时,跟随物体的相机可以在LateUpdate里实现。



        UpdateFixedUpdate的区别:

        update跟当前平台的帧数有关,而FixedUpdate是真实时间,所以处理物理逻辑的时候要把代码放在FixedUpdate而不是Update.

        Update是在每次渲染新的一帧的时候才会调用,也就是说,这个函数的更新频率和设备的性能有关以及被渲染的物体(可以认为是三角形的数量)。在性能好的机器上可能fps 30,差的可能小些。这会导致同一个游戏在不同的机器上效果不一致,有的快有的慢。因为Update的执行间隔不一样了。

        而FixedUpdate,是在固定的时间间隔执行,不受游戏帧率的影响。有点想Tick。所以处理Rigidbody的时候最好用FixedUpdate。

        PS:FixedUpdate的时间间隔可以在项目设置中更改,Edit->ProjectSetting->time  找到Fixedtimestep。就可以修改了。

        

        

        UpdateLateUpdate的区别

        在圣典里LateUpdate被解释成一句话:LateUpdate是在所有Update函数调用后被调用。这可用于调整脚本执行顺序。例如:当物体在Update里移动时,跟随物体的相机可以在LateUpdate里实现。这句我看了云里雾里的,后来看了别人的解释才明白过来。

        LateUpdate是晚于所有Update执行的。例如:游戏中有2个脚步,脚步1含有Update和LateUpdate,脚步2含有Update,那么当游戏执行时,每一帧都是把2个脚步中的Update执行完后才执行LateUpdate 。虽然是在同一帧中执行的,但是Update会先执行,LateUpdate会晚执行。

        现在假设有2个不同的脚本同时在Update中控制一个物体,那么当其中一个脚本改变物体方位、旋转或者其他参数时,另一个脚步也在改变这些东西,那么这个物体的方位、旋转就会出现一定的反复。如果还有个物体在Update中跟随这个物体移动、旋转的话,那跟随的物体就会出现抖动。 如果是在LateUpdate中跟随的话就会只跟随所有Update执行完后的最后位置、旋转,这样就防止了抖动。

        做一个相机跟随主角的功能时,相机的位置调整写在LateUpdate(),老是不明白,看官方的SmoothFollow相机跟随写在Update()中


新方法函数学习:

void Shoot()

    {

        //timer -= shootRate;

        timer = 0f;

        gunAudio.enabled = true;

        gunAudio.Play();

        gunLight.enabled = true;

        gunParticle.Play();

        gunLine.enabled = true;

        gunLine.SetPosition(0, transform.position);

        Ray ray = new Ray(transform.position, transform.forward);

        RaycastHit hitInfo;

        if(Physics.Raycast(ray, out hitInfo))

        {

            gunLine.SetPosition(1, hitInfo.point);

            if (hitInfo.collider.tag == Tags.Enemy)

                hitInfo.collider.GetComponent<EnemyHeath>().TakeDamage(10, hitInfo.point);

        }

        else

        {

            gunLine.SetPosition(1, transform.position + transform.forward * 100);

        }

    }


//控制转向

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo, 200, groundLayerIndex))

        {

            Vector3 target = hitInfo.point - transform.position;

            target.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation(target);

            GetComponent<Rigidbody>().MoveRotation(newRotation);

            //transform.LookAt(target);

        }

//敌人移动 AI导航

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

 

public class EnemyMove : MonoBehaviour {

 

    private NavMeshAgent Agent;

    //public Transform player;

    private Animator anim;

    private GameObject player;

 

// Use this for initialization

void Start () {

        Agent = GetComponent<NavMeshAgent>();

        anim = GetComponent<Animator>();

        player = GameObject.FindGameObjectWithTag(Tags.Player);

}

// Update is called once per frame

void Update () {

        if (Vector3.Distance(transform.position, player.transform.position) < 1.6f)

        {

            //Agent.Stop();

            Agent.isStopped = true;

            anim.SetBool("Move", false);

        }

        else

        {

            //Agent.Resume();

            Agent.isStopped = false;

            Agent.SetDestination(player.transform.position);

            anim.SetBool("Move",true);

        }

}

}


//相机跟随,用Lerp方法

void FixedUpdate () {

        transform.position = Vector3.Lerp(transform.position, Player.position + offset, smoothing * Time.deltaTime);



猜你喜欢

转载自blog.csdn.net/iov3Rain/article/details/80003358
今日推荐