目前本人使用过的对象移动方法有以下几种:
使用playercontroller自带的move和simplemove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCotroller: MonoBehaviour
{
CharacterController player;
// Start is called before the first frame update
void Start()
{
player = getComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//水平轴
float horizontial = Input.GetAxis("Horizontal");
//竖直轴
float vertical = Input.GetAxis("Vertical");
//创建成一个方向向量
Vector3 speed= new Vector3(horizontial *0.1f,0, vertical *0.1f);
Debug.DrawRay(transform.position ,speed, Color.red);
player.Move(speed* Time.deltaTime);
//或者player.SimpleMove(speed)
}
}
初次使用这两者的时候吓了我一跳,Move()移动速度太快了,加之本人很久没有接触C#了,总把0.2f写成0.2导致vrctor3与double不能相乘的错误。相比之下SimpleMove给我留下了一个好印象。SimpleMove()程序结算是按秒(每秒移动所给矢量speed),而Move()中是每帧结算(每帧移动所给矢量speed)。不过Move()能够通过jump更改Y轴(高度)数据。
以上两者的位移都是以世界为参考系,对象自身的旋转不会影响移动效果
根据本人的使用经验,这两者的位移结算优先级低于部分cube结算优先级,也就是说用Move全速撞向一个方块,哪怕地面没有摩擦力cube也不会被撞动。有趣的是,如果撞击对象是球体、圆柱等具有滚动性质的物体时不会出现这种情况,但碰撞效果十分感人,让我不得不怀疑这里之所以撞得动是因为有BUG。
使用transform.translate();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCotroller : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//水平轴
float horizontial = Input.GetAxis("Horizontal");
//竖直轴
float vertical = Input.GetAxis("Vertical");
//创建成一个方向向量
Vector3 direction = new Vector3(horizontial * 0.1f, 0, vertical * 0.1f);
Debug.DrawRay(transform.position ,direction, Color.red);
transform.Translate(direction);
}
}
在这儿强烈安利大家在使用Translate时给对象加个CharacterController,然后你就会得到一个总是螺旋升天的对象(doge)。
transform的使用就很符合大部分人的直觉,做出来的效果也很接近于现实。也没有之前Move()的关于立方体的毛病。
transmit的参考系是对象自身,如果输入额向量是(1,0,0)那么对象会沿着自身X轴方向移动。
其他几种还没用到,用到再说