实验目的
(1)掌握3D物体中transform组件中rotation属性表示的意思。
(2)掌握通过脚本代码让物体旋转的方法——Rotate()。
(3)掌握通过脚本让物体围绕其他物体旋转的方法——RotateAround()。
(4)掌握“看向”某个物体的方法——LookAt()。
实验内容及步骤
(1)在Unity中打开新的场景,通过已给出的贴图制作月球、地球,并调整它们的对应位置。
(2)地球自转代码:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Earth : MonoBehaviour
{
public float speed = 1;
float currentAngle = 0;//当前沿着某轴旋转后与原来没有旋转的时候角度的夹角
float rotateAngle = 0;//每次沿着某轴又旋转的角度值
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rotateAngle = speed * Time.deltaTime;
transform.Rotate(new Vector3(0, rotateAngle, 0),Space.Self);
}
}
(3)月球围绕地球转和看向地球的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moon : MonoBehaviour
{
public Transform earth;
public float speed;
private float rotateAngle = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rotateAngle = Time.deltaTime * speed;
transform.RotateAround(earth.position, earth.transform.up, rotateAngle);
transform.LookAt(earth.position, earth.transform.up);
}
}
实验总结
1、收获
- rotate函数的意义在于定义了一种将元素围绕一个定点旋转而不变形的转换。
- lookAt函数可以计算出相机朝向的向量,并将其转换为一个旋转矩阵。然后,这个旋转矩阵被应用到场景中的物体上,使其旋转到正确的视角。这样,我们可以通过改变相机的位置、目标位置和上向量来实现视角的变换,从而观察3D场景中的不同角度。
- rotateAround函数在AR(增强现实)和VR(虚拟现实)中,主要用于实现对象或场景在三维空间中的旋转效果。
2、存在的问题
- 对于物体材质方面不太能理解,材质是可以通过肉眼看到的吗。后来了解到材质参数包括颜色,透明度,光泽度,折射率等。
- 像创建月亮这种物体就需要考虑到光泽度和折射率的概念。调整材质参数是获得更真实效果的关键。