基础知识随堂练习总结+扇形攻击代码+摄像机跟随代码

1.rotation是什么类型的 让一个物体旋转 45,90,90 代码的书写?
四元数类型 transform.ratotion = Quaternion.euler(45,90,90);

2.触发和碰撞的区别和联系 tag 和层的用法
区别:触发需要勾选Is Trigger 可以穿透 tag多用于触发和碰撞,层多用于射线.

3.实例化物体 instantiate() 参数有几个 若一个参数返回类型是什么 三个参数返回类型有几个
可以一个参数也可以三个参数 参数类型为 object vector3 rotation

4.对一个按钮动态添加响应时间的代码

gameobject.GetComponent<button>.Addlisten(方法);

5.协同可以传递参数吗? 参数类型是什么
可以传递 类型为object

6.在碰撞检测中我们是怎么样判断是哪种物体进入我的碰撞里面
通过tag进行判断 对象名字也可以

7.如何捕捉在程序运行时候的错误
try catch

8.脚本动态挂载代码 脚本禁用代码
enabled组件的激活 禁用 SetActive 是 GameObject中的方法用来激活 禁用游戏对象.

9.写出得到某一个层的两种方式 1<<X
LayerMask.GetMask("");

10.Input.GetAxisRow 和 Input.GetAxis区别
GetAxisRaw不使用平滑滤波器
就是说GetAxis可以返回-1到1之间的数,比如检测到按键按下的时候,由0不断增加过渡到1
GetAxisRaw只能返回-1,0,1三个值

扇形攻击代码

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

public class S4Player : MonoBehaviour {

	void Start () {
	}
	void Update () {
		if (Input.GetKeyDown(KeyCode.P))  //玩家按P键进行攻击
		{
			AttackEnemy();
		}
	}
	void AttackEnemy()
	{
		//获取在球形范围里面的碰撞框
	Collider[] _colliders =	Physics.OverlapSphere(transform.position, 5);
		//   符合要求的敌人列表
		List<GameObject> AttackdList = new List<GameObject>();
		//筛选完毕
		foreach (var v in _colliders)
		{
			//判断是否在攻击角度里面 
			if (UmbrellaAttact(transform, v.transform, 60, 5))
			{
				AttackdList.Add(v.gameObject);
				Debug.LogError(v.name);
			}
		}
           //进行伤害计算
		foreach (GameObject o in AttackdList)
		{
			//获取距离
			float dis = Vector3.Distance(transform.position, o.transform.position);
         //计算减血数值多少 离玩家越近收到的伤害越高
			float blood = 5 * 10 / dis;
			//进行减血
			o.GetComponent<S4Enemy>().Attacked(blood);
		}
	}
	//封装好的攻击方法 返回bool值
	private bool UmbrellaAttact( Transform  attacker ,Transform attacked  ,float  angle, float radius)
	{
        
		Vector3 deltaA = attacked.position - attacker.position;
        
		float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacker.forward)) * Mathf.Rad2Deg;
        
		if (tmpAngle < angle * 0.5f && deltaA.magnitude < radius)
		{
			return true;
		}
		return false;
	}

}

摄像机跟随代码

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

public class MainCameraCtr : MonoBehaviour {
    public int Speed;//定义速度变量
    public Transform TagTransform;//要跟随的物体
    private Vector3 offset;//一个向量,即摄像机与物体间的差值

	// Use this for initialization
	void Start () {
        Speed = 5;//定义速度为5
        offset = transform.position - TagTransform.position;//摄像机与物体间的差值
    }
	
	// Update is called once per frame
	void Update () {
		
	}
     void LateUpdate()
    {
        Vector3 newpos = TagTransform.position + offset;//摄像机新的位置信息为跟随物体的坐标+差值
        //线性插值,在 Speed * Time.deltaTime摄像机位置移动从之前位置开始到newpos结束
        transform.position = Vector3.Lerp(transform.position, newpos, Speed * Time.deltaTime);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/83176313
今日推荐