Unity例程

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 () {
		
	}
}

6.UI中image添加操作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
//interface
public class UIEventManager2 : MonoBehaviour//, IPointerDownHandler,IPointerClickHandler,IPointerUpHandler,IPointerEnterHandler,IPointerExitHandler
    ,IBeginDragHandler,IDragHandler,IEndDragHandler,IDropHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");
    }

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("OnDrop");
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("OnEndDrag");
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("OnPointerClick");
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("OnPointerDown");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("OnPointerEnter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("OnPointerExit");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("OnPointerUp");
    }
}
  • 先添加接口,然后点击接口,自动生成需要完善的代码
  • https://docs.unity3d.com/2020.1/Documentation/Manual/SupportedEvents.html

7.从网上或者本地下载文件

  • WWW
  • https://docs.unity3d.com/2020.1/Documentation/ScriptReference/WWW.html

8.组件CharacterController

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

public class PlayerCC : MonoBehaviour {

    public float speed = 3;
    private CharacterController cc;

	// Use this for initialization
	void Start () {
        cc = GetComponent<CharacterController>();
        
	}
	
	// Update is called once per frame
	void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //控制移动,有重力
        cc.SimpleMove(new Vector3(h, 0, v) * speed);
        //cc.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);//忽视重力
        Debug.Log(cc.isGrounded);
	}

    //碰撞事件检测
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Debug.Log(hit.collider);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值