此代码适用于电梯类型的平台,一旦玩家站在平台上,它就会通过向平台施加力将玩家“带上”。
问题是,当力产生时,刚体(玩家)在电梯移动时不会移动。代码是使用 Unity 5 用 C# 编写的。在代码中,玩家被分配了公共“rb”,并包含一个刚体。动画是一个简单的动画片段,可以让电梯向上移动。有任何想法吗?感谢您的时间和提前回答。
电梯是运动学的,玩家不是。
using UnityEngine;
using System.Collections;
/*This script activates when the player steps on the elevator, as it takes them up a floor.*/
public class ElevatorMovementScript : MonoBehaviour
{
private bool elevatorUp = false;
public Animation anim;
public int elevatorDelay = 5;
public int force = 800;
public Rigidbody rb;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animation>();
}
// Update is called once per frame
void Update ()
{
}
/*Checks if the player has stepped onto the elevator. If the player has, it waits five seconds, and then pushes the player up.*/
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player" && !elevatorUp)
{
Invoke("AnimationPlay",elevatorDelay);
elevatorUp = true;
}
}
/*Plays the animation of the player going up. Used for the 'Invoke' method.*/
void AnimationPlay()
{
rb.AddForce(transform.up * force);
Debug.Log (transform.up * force);
anim.Play ("Up");
}
}