我正在尝试在退出游戏中的“触发器”区域时使对象掉线。那么当我进入触发区时,对象开始浮动会发生什么。但是当我离开区域时,物体不会掉落到地面,当我再次进入触发区时,它会在半空中停止,它会继续漂浮吗?
这是我的浮动脚本:
using UnityEngine;
using System.Collections;
public class Floatinga : MonoBehaviour {
public float horizontalSpeed;
public float verticalSpeed;
public float amplitude;
private Vector3 tempPosition;
void Start ()
{
tempPosition = transform.position;
}
void FixedUpdate ()
{
tempPosition.x += horizontalSpeed;
tempPosition.y += verticalSpeed;
transform.position = tempPosition;
}
}
然后当我进入或退出时:
using UnityEngine;
using System.Collections;
public class Floating : MonoBehaviour {
public GameObject otherObject;
// Use this for initialization
void Start () {
otherObject.GetComponent<Floatinga>().enabled = false;
}
void OnTriggerEnter()
{
otherObject.GetComponent<Floatinga>().enabled = true;
}
// Update is called once per frame
void OnTriggerExit ()
{
otherObject.GetComponent<Floatinga>().enabled = false;
}
}
所以我不确定我做错了什么?任何人都可以帮忙吗? 感谢
答案 0 :(得分:1)
只需添加一个刚体并将useGravity设置为true。