我有一个简单的项目,我在其中产生了多个小行星,它们往往会被行星吸引。我想添加一个简单的轨迹来增强视觉效果。当我手动添加小行星然后添加组件“轨迹渲染器”并选择所需的材料时,这非常简单。但我无法弄清楚如何将其添加到脚本中。这是我现在的代码:
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(FauxGravityBody))]
public class Spawner : MonoBehaviour {
public GameObject meteorPrefab;
public float distance = 20f;
public float time = 10f;
private GameObject meteor;
public TrailRenderer trail;
void Start ()
{
StartCoroutine(SpawnMeteor());
}
IEnumerator SpawnMeteor()
{
Vector3 pos = Random.onUnitSphere * distance;
meteor = Instantiate(meteorPrefab, pos, Quaternion.identity);
meteor.AddComponent<FauxGravityBody>();
meteor.AddComponent<DestroyMeteor>();
meteor.AddComponent<SphereCollider>();
meteor.AddComponent<TrailRenderer>();
yield return new WaitForSeconds(time);
StartCoroutine(SpawnMeteor());
}
}
这确实将轨迹添加到生成的对象中,但它使用的是默认的粉红色轨迹。我想要的跟踪材料位于文件夹“Assets/Effects/Trail.mat”中。如何在脚本中指定我想使用该特定材料?
亲切的问候。