Unity3d 本地转发 [英] Unity3d Local Forward

查看:34
本文介绍了Unity3d 本地转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在本地坐标中向前移动刚体,我的意思是,如果我旋转它,我希望它在他的本地 X 轴上移动.

I am trying to move a Rigidbody forward in local coordinates, I mean, if I rotate it I want it to move in his local X axis.

我试过这个,但它在全局坐标中移动:

I have tried this, but it moves in global coordinates:

Rigidbody player = GetComponent<Rigidbody>();

Vector3 movement = new Vector3 (1.0f, 0.0f, 0.0f);
movement = movement.normalized * 2 * Time.deltaTime;
player.MovePosition(transform.position + movement);

我不知道如何更改本地坐标.

I don't know how to make the change to local coordinates.

推荐答案

MovePosition 适用于世界空间,所以你必须这样做:

MovePosition works in world space, so you have to do this:

Rigidbody player = GetComponent<Rigidbody>(); // I really hope you're not doing this every frame, btw
float speed = 2f; // magic numbers are bad, move them to variables, at least
Vector3 movement = transform.forward * speed * Time.deltaTime;
player.MovePosition(transform.position + movement);

这篇关于Unity3d 本地转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆