Unity例程_1

1.实现敌人视线慢慢转向玩家

public class API_3_lookr : MonoBehaviour {

    public Transform emeny;
    public Transform player;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		//指向player的向量
        Vector3 direct = emeny.position - player.position;
        direct.y = 0;
        //将向量转换为quaternion,看的rotation
        Quaternion target = Quaternion.LookRotation(direct);
        //emeny慢慢转向player
        emeny.rotation = Quaternion.Slerp(emeny.rotation, target, Time.deltaTime);
	}
}

2.实现氮气加速效果

Rigidbody.AddForce(transform.forward * size);

3.鼠标碰撞检测

public class API_4_raydect : MonoBehaviour {

    private Camera camera1;

	// Use this for initialization
	void Start () {
		//获得第一个camera tagged "MainCamera"
        camera1 = Camera.main;
	}
	
	// Update is called once per frame
	void Update () {
		//将鼠标位置转化为ray
        Ray ray = camera1.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray, out hit);
        if (isCollider)
        {
            Debug.Log(hit.collider);
        }
	}
}

4.射线碰撞RayCast

public class API_1_raw : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        // 创建一个射线
        Ray ray = new Ray(transform.position + transform.forward, transform.forward);
        //获得是否发生碰撞
        bool isCollider1 = Physics.Raycast(ray);
        Debug.Log(isCollider1);
        //创建一个射线碰撞
        RaycastHit hit;
        //LayMask是指与那些Layer发生碰撞, out hit获得与射线发生碰撞的有关参数
        bool isCollider2 = Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("enemy1"));
        Debug.Log(hit.collider);
        Debug.Log(hit.point);

	}
}

5.UGUI 事件监听

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

public class UIEventManager : MonoBehaviour {

    public GameObject btnGameObject;
    public GameObject sliderGameObject;
    public GameObject dropDownGameObject;
    public GameObject toggleGameObject;

	// Use this for initialization
	void Start () {
        btnGameObject.GetComponent<Button>().onClick.AddListener(this.ButtonOnClick);
        sliderGameObject.GetComponent<Slider>().onValueChanged.AddListener(this.OnSliderChanged);
        dropDownGameObject.GetComponent<Dropdown>().onValueChanged.AddListener(this.OnDropDownChanged);
        toggleGameObject.GetComponent<Toggle>().onValueChanged.AddListener(this.OnToggleChanged);
    }

    void ButtonOnClick()
    {
        Debug.Log("ButtonOnClick");
    }
    void OnSliderChanged(float value)
    {
        Debug.Log("SliderChanged:" + value);
    }
    void OnDropDownChanged(Int32 value)
    {
        Debug.Log("DropDownChanged:" + value);
    }
    void OnToggleChanged(bool value)
    {
        Debug.Log("ToggleChanged:" + value);
    }

    // Update is called once per frame
    void Update () {
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ENABLE_UNITY_COLLECTIONS_CHECKS是Unity中的一个编译器宏定义,用于启用Unity Collections库中的线程和处置安全检查。这个宏定义可以确保在使用Unity Collections库时,对于线程安全和资源释放的问题进行检查,以避免潜在的错误。\[1\] Unity Collections库提供了一些关键的类数组类型,如NativeArray和NativeSlice,以及一些数据结构,如NativeList和NativeQueue,这些都受到ENABLE_UNITY_COLLECTIONS_CHECKS宏定义的影响。\[2\] 但是需要注意的是,ENABLE_UNITY_COLLECTIONS_CHECKS主要用于单元测试框架中的断言,而不是用于生产代码的测试。因此,在生产代码中使用这个宏定义可能会导致性能下降,因为它会增加额外的检查和开销。\[3\] #### 引用[.reference_title] - *1* *3* [unity断言_Unity断言库](https://blog.csdn.net/culiao6493/article/details/108642656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [[Unity ECS] Unity Collections Package](https://blog.csdn.net/u013716859/article/details/122278432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值