我想开发一个游戏(2D),你可以放置“时间泡泡”,其中每个物体都在内部减慢它的运动。
在“时间泡沫”的脚本中,我尝试了以下内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeBubble : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D other)
{
GameObject temp = GameObject.Find(other.name);
temp.GetComponent<Rigidbody>().velocity = Vector3.zero;
temp.GetComponent<Rigidbody>().angularVelocity = Vector3.right * 0;
}
}
Wich不工作。
有人知道如何减慢泡沫中的所有元素吗?
答案 0 :(得分:0)
观察!
如果你有另一个改变速度的脚本,就像一个charachter控制器,你不能从这个脚本设置速度,你需要将speedReductionFactor传递给你的Charachter控制器,然后用它来更新那里的速度和输入逻辑,我会把它留给你。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeBubble : MonoBehaviour {
[Range(0,1)]
public float speedReductionFactor = .5f;
List <GameObject> objectsInside;
Rigidbody2D rb;
void Start ()
{
objectsInside = new List<GameObject>();
}
void OnTriggerEnter2D(Collider2D other) {
rb = other.gameObject.GetComponent<Rigidbody2D>();
if (rb == null)
return;
rb.velocity *= speedReductionFactor;
objectsInside.Add(other.gameObject);
}
void OnTriggerExit2D (Collider2D other){
rb = other.gameObject.GetComponent<Rigidbody2D>();
if (rb != null && objectsInside.Contains(other.gameObject)) {
rb.velocity *= (1 / speedReductionFactor);
objectsInside.Remove(other.gameObject);
}
}
}