Unity练习题汇总

Unity引擎 · 01-02 · 390 人浏览

1.在屏幕上点击一下鼠标,则在对应的世界坐标位置,创建一个cube出来
知识点 屏幕坐标转世界坐标

if (Input.GetMouseButtonDown(0))
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
Vector3 v = Input.mousePosition;
v.z = 20;
obj.transform.position = Camera.main.ScreenToWorldPoint(v);
}

2.一个物体A,不管它在什么位置,写一个方法,只要执行这个方法就可以在它的前方创建3个球体
知识点 本地坐标转世界坐标

位置是(0,0,1),(0,0,2),(0,0,3)
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = this.transform.TransformPoint(new Vector3(0, 0, 1));
obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = this.transform.TransformPoint(new Vector3(0, 0,2));
obj =GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = this.transform.TransformPoint(new Vector3(0, 0, 3));

3.插值一个物体追逐另外一个物体(先快后慢和匀速运动)
public class mathf : MonoBehaviour
{

float speed = 1.5f;
private float time;
public Transform target;
public Vector3 pos;
private Vector3 bNowPos;
private Vector3 startPos;
// Update is called once per frame
void Update()
{
    //
    //先快后慢
    //pos = transform.position;
    //pos.x = Mathf.Lerp(pos.x, target.position.x, Time.deltaTime * speed);
    //pos.y = Mathf.Lerp(pos.y, target.position.y, Time.deltaTime * speed);
    //pos.z = Mathf.Lerp(pos.z, target.position.z, Time.deltaTime * speed);
    //this.transform.position = pos;


    //匀速运动 清0
    if (bNowPos!=target.position)
    {
        time = 0;
        bNowPos = target.position;
        startPos = this.transform.position;
    }

    time += Time.deltaTime;
    pos.x = Mathf.Lerp(startPos.x, bNowPos.x, time);
    pos.y = Mathf.Lerp(startPos.y, bNowPos.y, time);
    pos.z = Mathf.Lerp(startPos.z, bNowPos.z, time);
    this.transform.position = pos;
}

}

4.实现一个物体按曲线移动(正弦或者余弦)

public float moveSpeed = 3;//面朝向移动速度
public float changespeed = 5;//左右曲线移动变化的速度
public float changeSize = 3;//左右曲线移动距离控制
private float time = 0;

// Update is called once per frame
void Update()
{
    //实现一个物体按曲线移动(正弦或者余弦)
    //面朝向移动
    this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    //左右曲线运动
    time += Time.deltaTime * changespeed;
    //Mathf.Sin 这个函数是sin,别写成sign了
    this.transform.Translate(Vector3.right * changeSize * Time.deltaTime * Mathf.Sin(time));
}
  1. 用向量相关知识,实现摄像机跟随(摄像机不设置对象子物体)
    摄像机一直再物体的后方4米,向上偏7米的位置

    public float zOffset=8;
    public float yOffset=7;
    public Transform target;

    // Update is called once per frame
    void LateUpdate()
    {

    //先朝目标对象的 面朝向的反方向平移4米 再朝目标的头顶位置 平移7米
    this.transform.position = target.position + (-target.forward) * zOffset + target.up * yOffset;
    
    this.transform.LookAt(target);

    }

6.//当一个物体B在物体A前方45度角范围内,并且离A只有5米的距离时,在控制台打印发现入侵者

方法一
//if (Vector3.Distance(this.transform.position, B.transform.position) <= 5)
//{
// //1.点乘算出方向向量
// float dotRes = Vector3.Dot(this.transform.forward, (B.transform.position - this.transform.position).normalized);
// //2.通过反余弦函数算出夹角
// if (Mathf.Acos(dotRes) * Mathf.Rad2Deg<=22.5f)
// {
// print("发现入侵者");
// }

//}

方法二
if (Vector3.Distance(this.transform.position,B.transform.position)<=5)
{

 if (Vector3.Angle(this.transform.forward,(B.transform.position-this.transform.position).normalized)<=22.5)
 {
     print("发现入侵者");
 }

}
7.判断一个物体B在物体A的左上,左下,右上,右下哪个方位
Vector3 c = Vector3.Cross(A.forward, B.position - A.position);
if (c.y>0)
{

//左边
float dotRes = Vector3.Dot(A.transform.forward, B.position - A.position);
if (dotRes > 0)
{
    //前方
    print("B在A右前");
}
else
{
    //后方
    print("B在A右后");
}

}
else if (c.y<0)
{

//右边
float dotRes = Vector3.Dot(A.transform.forward, B.position - A.position);
if (dotRes > 0)
{
    //前方
    print("B在A左前");
}
else
{
    //后方
    print("B在A左后");
}

}

  1. 当一个物体B在物体A左前方20度角或右前方30度范围内,并且离A只有5米距离时
    在控制台打印打印发现入侵者
    public Transform A;
    public Transform B;
    float dcRes;
    Vector3 ccRes;
    dcRes = Vector3.Dot(A.forward, B.position - A.position);
    ccRes = Vector3.Cross(A.forward, B.position - A.position);
    if (Vector3.Distance(A.position,B.position)<=5)
    {
    //B在A前方
    if (ccRes.y >= 0)
    {
        if (Vector3.Angle(A.forward, B.position - A.position) <= 30)
        {
            print("B在A右30度内");
        }
      
      
    }
    else
    {
        if (Vector3.Angle(A.forward, B.position - A.position) <= 20)
        {

              print("B在A左20度内");
        }
}

}
log.webp
g.jpg

  1. 111 01-09

    1111

Theme Jasmine by Kent Liao