Unity3D 的LookAt() 与 LookRotation()方法的比较

LookAt(),定义: 其定义在UnityEngine.Transform类中,

public void LookAt(Vector3 worldPosition);

public void LookAt(Transform target);

用法: 一:transform.LookAt(new Vector3(1,1,1));

使游戏对象看向该坐标点(游戏对象的z轴指向(1,1,1));

二:transform.LookAt(gameobject.transform)

使游戏对象看向gameobject的transform的position;

在场景中创建cube与Sphere两个游戏对象,将脚本挂载到Cube上;

using UnityEngine;

public class Test : MonoBehaviour {

    private Transform other;//Sphere游戏对象

    void Awake()
    {
        //找到Sphere游戏对象
        other = transform.Find("/Sphere").GetComponent<Transform>();
    }

	void Start()
    {
        //Cube的z轴指向(1,1,1)点
//        transform.LookAt(new Vector3(1, 1, 1));
    }

    void Update()
    {
        //画线调试,由Cube的postion指向Sphere的postion
        Debug.DrawLine(transform.position,other.position,Color.cyan);
        //Cube的z轴指向Sphere,(指向了Sphere游戏对象的Transform组件的position值,也是一个Vector3类型的值) 
        transform.LookAt(other);
    }
}

运行结果:

Cube(蓝色箭头为Cube的z轴)指向了Sphere的中心;

#运行过程中的有趣发现#

为Cube添加了Rigidbody组件后,运行时Cube在不停摆动,且z轴一直指向Sphere的中心。其原因应为将LookAt()方法放于Update()方法中,使其每一帧都指向Sphere,并且Cube并没有贴合地面,在重力作用下往下掉的同时由于LookAt每一帧都在修复其指向,从而出现这种情况;


LookRotation() 定义: 其为定义在UnityEngine.Quaternion中的静态方法,

public static Quaternion LookRotation(Vector3 forward);

用法: Quaternion.LookRotation(new Vector3(1,1,1));

获取一个向量所表示的方向(将一个向量转为该向量所对应的方向);

使游戏对象面向(1,1,1)这个向量

在场景中创建一个cube_1与Sphere,将脚本挂在cube_1上

using UnityEngine;

public class LookRotation : MonoBehaviour {

    private Transform other;//Sphere游戏对象

    void Awake()
    {
        //找到Sphere游戏对象
        other = transform.Find("/Sphere").GetComponent<Transform>();
    }

    void Start()
    {
        //将Cube_1的z轴指向坐标原点(Vector.zero)到 Vector3(2,2,2)所对应的向量方向
        transform.rotation = Quaternion.LookRotation(new Vector3(2, 2, 2));
        //将Cube_1的z轴指向原点到other.position对应向量的方向
        transform.rotation =  Quaternion.LookRotation(other.position);
    }

    void Update()
    {
        //画线调试,画出从Cube_1到Sphere的线段
        Debug.DrawLine(transform.position, other.position,Color.blue);
        //画线调试,画出坐标原点到Sphere的射线在cube_1上的表现,即画出向量方向
        Debug.DrawRay(transform.position, other.position,Color.red);
        Move();
    }

    void Move()
    {
        float horizontal = Input.GetAxis("Horizontal");//获取水平偏移量(x轴)
        float vertical = Input.GetAxis("Vertical");    //获取垂直偏移量(z轴)
        //将水平偏移量与垂直偏移量组合为一个方向向量
        Vector3 direction = new Vector3(horizontal, 0, vertical);
        //判断是否有水平偏移量与垂直偏移量产生
        if (direction != Vector3.zero)
        {
            //将游戏对象的z轴转向对应的方向向量
//            transform.rotation = Quaternion.LookRotation(direction);
            //对上一行代码进行插值运算则可以将转向表现得较平滑
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), 0.3f);
            //将游戏对象进行移动变换方法则可以实现简单的物体移动
            transform.Translate(Vector3.forward * 5 * Time.deltaTime);
        }
    }
}
运行结果:


cube_1的z轴与红色这条射线重合,而并非指向sphere(并未与蓝色射线重合);

用LookRotation()可以实现游戏对象转向;参考上面的Move()方法;

小结:

LookAt()与LookRotation()的参数都相似,但前者是将游戏对象的z轴指向参数所表示的那个点,而后者是将游戏对象的z轴指向参数所表示的向量的方向;

  • 20
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值