我正在Unity编写2D平台游戏,我试图让玩家留在移动平台上。我已经完成了一两天的搜索和修补工作,而且我没有任何运气。留在移动平台上
基本上,我被告知要尽量让角色在触摸时随着平台移动。首先,如果我使用与OnTriggerEnter()相关的任何东西,玩家就可以通过该平台。如果我做OnCollisionEnter()(在播放器上有一个CharacterController,在平台上有一个BoxCollider),根本没有任何事情发生。这些是我发现的最重要的两件事。另一个是用平台来抚养玩家,但这显然会造成“问题”(经常说,从来没有解释过)。
那么,我该如何让玩家留在移动平台上呢?下面是移动平台的代码:
public class MovingPlatform : MonoBehaviour
{
private float useSpeed;
public float directionSpeed = 9.0f;
float origY;
public float distance = 10.0f;
// Use this for initialization
void Start()
{
origY = transform.position.y;
useSpeed = -directionSpeed;
}
// Update is called once per frame
void Update()
{
if(origY - transform.position.y > distance)
{
useSpeed = directionSpeed; //flip direction
}
else if(origY - transform.position.y < -distance)
{
useSpeed = -directionSpeed; //flip direction
}
transform.Translate(0,useSpeed*Time.deltaTime,0);
}
,这里是对玩家的动作(在更新)的代码:与我怎么样,我认为它可能要做:
CharacterController controller = GetComponent<CharacterController>();
float rotation = Input.GetAxis("Horizontal");
if(controller.isGrounded)
{
moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
//running code
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
{ running = true; }
else
{ running = false; }
moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
//jump code
if(Input.GetButtonDown("Jump"))
{
//moveDirection.y = jumpHeight;
jump();
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
编辑m定义了玩家和平台,但我尝试了不同的组合。如果平台是一个触发器(在其对撞机上),玩家就会一直贯彻它。如果没有,我不能使用OnTrigger函数。我有一个固定在播放器和平台上的刚体,但它似乎没有影响任何东西。当玩家在某些设置上进入平台时,他会不停地跳动,而且往往会结束。
我从来没有使用Unity3D,但有可能像做'moveDirection + = theplatform。moveDirection'当角色在平台上? – mbeckish 2013-03-26 17:59:05
我试过了,但我似乎遇到了OnTrigger和OnCollision函数的问题,这些函数将负责“玩家在平台上”时的情况。我认为这与我如何定义我的对象有关,但我不确定如何定义它们以使其可行。 – muttley91 2013-03-26 18:02:02
此外,你需要确保角色的moveDirection.y永远不会比你的平台的moveDirection.y更少,否则你会遇到(除非Unity3D框架自动为你处理?) – mbeckish 2013-03-26 18:03:10