Unity-移动旋转

  1. 云栖社区>
  2. 博客>
  3. 正文

Unity-移动旋转

二哈卖豆腐 2019-03-28 16:16:56 浏览704
展开阅读全文

move

CharacterController:
Rigidbody:AddForce,MovePosition
Translate:不检测碰撞
rotate

Rotation:
Rigidbody:MoveRotation
示例

王者荣耀式:

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

public class PlayerCtrl : MonoBehaviour
{
public float speedmove = 1;
public float speedrot = 20;

private CharacterController cc;
private float ver;
private float hor;
private Vector3 dir;
private Quaternion qua;

private void Start()
{
cc = GetComponent();
}

private void Update()
{

}

private void FixedUpdate()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
dir = new Vector3(hor, 0, ver).normalized;
cc.SimpleMove(dir * speedmove);
//transform.position = Vector3.Lerp(transform.position,transform.position+dir, Time.deltaTime * speedmove);
if (hor != 0 || ver != 0)
{
qua = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Lerp(transform.rotation, qua, Time.deltaTime * speedrot);
}
}
}
Vector3.Angle(Vector3,Vector3); //向量夹角
Vector3.ClampMagnitude(Vector3, maxLength); //复制一个向量,指定最大长度

//差乘(右手大拇指确定方向)
Vector3.Cross(Vector3,Vector3);

Vector3.Distance(Vector3, Vector3); //距离
Vector3.Dot(Vector3, Vector3); //点乘
Vector3.Project(Vector3, Vector3); //投影
Vector3.Reflect(Vector3, Vector3); //反射向量
Quaternion.Angle(a, b); //夹角
Quaternion.AngleAxis(angle, axis); //绕轴转角度
Quaternion.Dot(a, b);
Quaternion.Euler(euler);
Quaternion.FromToRotation(from, to);
Quaternion.Inverse(a); //反向
Quaternion.LookRotation(v3); //看向
Quaternion.RotateTowards(from, to, angle); //转向
Quaternion.Slerp(a, b, t);
注意:

LookRotation:
看向一个向量,是一个方向

LookAt:
看向一个坐标,一个点

更多unity2018的功能介绍请到paws3d爪爪学院查找。

网友评论

登录后评论
0/500
评论
二哈卖豆腐
+ 关注