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 () {
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值