unity转身代码_Unity中物体的旋转方法代码

第一种:

transform.Rotate(new Vector3(90, 0, 0));

//重载方式重载方式一 第一个参数是 x轴 旋转度数,第二个参数是 Y 轴 旋转度数 ,第三个参数是Z轴旋转度数 ,

//第四个参数是自身还是世界坐标

public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);

//重载方式二 第一个参数沿着谁旋转 第二个参数是旋转的角度 第三个参数是自身还是世界坐标

public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

// 重载方式三 第一个参数沿着谁旋转 第二个参数是沿着自身还是世界坐标 一般的默认世界旋转

public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);

using UnityEngine;

public class ExampleClass : MonoBehaviour

{

public float speed; // 速度

void Update()

{

//以每秒1度的速度围绕其局部X轴旋转对象

transform.Rotate(Vector3.right * Time.deltaTime*speed);

// 以世界坐标的Y 轴进行旋转

transform.Rotate(Vector3.up * Time.deltaTime, Space.World*speed);

}

}

第二种: 四元数旋转

transform.rotation = Quaternion.Euler(45, 45, 45);

// 重载方式是一个参数是vector3 类型的数据   以下那两个只是形式不一样

public static Quaternion Euler(float x, float y, float z);

public static Quaternion Euler(Vector3 euler);

using UnityEngine;

public class Example : MonoBehaviour

{

void Start()

{

// 绕着Y 轴旋转30 度

Vector3 rotationVector = new Vector3(0, 30, 0);

Quaternion rotation = Quaternion.Euler(rotationVector);

}

}

第三种种: 通过四元数   一个物体以一定的速度转向目标物体

//通过t在a和b之间进行球面插值,将参数t夹在[0,1]范围内。

transform.rotation = Quaternion.Slerp();

// 重载方式三个参数返回一个 Quaternion 值也就是一个transform.rotation 类型

//第一个参数是起点 第二是终点 第三个是所需要的时间

public static Quaternion Slerp(Quaternion a, Quaternion b, float t);

//这个用个官网示例

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour

{

public Transform from;

public Transform to;

private float timeCount = 0.0f;

void Update()

{

transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, timeCount);

timeCount = timeCount + Time.deltaTime;

}

}

第四种:

// 绕着某一物体旋转

transform.RotateAround();

// 回调函数  第一个参数是绕着某一物体旋转,第二个是旋转轴向 第三个参数是 旋转的速度

public void RotateAround(Vector3 point, Vector3 axis, float angle);

// 官网示例代码

using UnityEngine;

public class Example : MonoBehaviour

{

void Update()

{

// 让物体以20度/秒的速度绕着地球原点旋转。.

transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);

}

}

第五种:欧拉角旋转 有旋转的最大值  还有万向锁,但是本人不习惯使用欧拉角旋转,大部分使用的都是四元数旋转

其实欧拉角和四元素旋转各有各的好处,最适合哪个就用哪个

using UnityEngine;

public class Example : MonoBehaviour

{

// 使用欧拉角指定一个绝对旋转

float yRotation = 5.0f;

void Start()

{

// 打印旋转x最大值

print(transform.eulerAngles.x);

// 打印旋转y最大值

print(transform.eulerAngles.y);

// 打印旋转z最大值

print(transform.eulerAngles.z);

}

void Update()

{

yRotation += Input.GetAxis("Horizontal");

transform.eulerAngles = new Vector3(10, yRotation, 0);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值