publicclassAPI_3_lookr:MonoBehaviour{publicTransform emeny;publicTransform player;// Use this for initializationvoid Start (){}// Update is called once per framevoid Update (){//指向player的向量Vector3 direct = emeny.position - player.position;
direct.y =0;//将向量转换为quaternion,看的rotationQuaternion target = Quaternion.LookRotation(direct);//emeny慢慢转向player
emeny.rotation = Quaternion.Slerp(emeny.rotation, target, Time.deltaTime);}}
2.实现氮气加速效果
Rigidbody.AddForce(transform.forward * size);
3.鼠标碰撞检测
publicclassAPI_4_raydect:MonoBehaviour{privateCamera camera1;// Use this for initializationvoid Start (){//获得第一个camera tagged "MainCamera"
camera1 = Camera.main;}// Update is called once per framevoid Update (){//将鼠标位置转化为rayRay ray = camera1.ScreenPointToRay(Input.mousePosition);RaycastHit hit;bool isCollider = Physics.Raycast(ray,out hit);if(isCollider){
Debug.Log(hit.collider);}}}
4.射线碰撞RayCast
publicclassAPI_1_raw:MonoBehaviour{// Use this for initializationvoid Start (){}// Update is called once per framevoid Update (){// 创建一个射线Ray ray =newRay(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;publicclassUIEventManager:MonoBehaviour{publicGameObject btnGameObject;publicGameObject sliderGameObject;publicGameObject dropDownGameObject;publicGameObject toggleGameObject;// Use this for initializationvoid 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);}voidButtonOnClick(){
Debug.Log("ButtonOnClick");}voidOnSliderChanged(floatvalue){
Debug.Log("SliderChanged:"+value);}voidOnDropDownChanged(Int32value){
Debug.Log("DropDownChanged:"+value);}voidOnToggleChanged(boolvalue){
Debug.Log("ToggleChanged:"+value);}// Update is called once per framevoid Update (){}}