나는 Unity3D 프로젝트의 라인 렌더러 lineRenderer0_2 두 개의 게임 오브젝트 (sphereObject0 및 sphereObject2)를 연결하기 위해 노력하고있어.런타임시 라인 렌더러의 시작 및 끝 위치를 업데이트하는 방법은 무엇입니까?
처음에는 선이 잘 나옵니다. 내 문제는 sphereObject0 및 sphereObject2의 위치와 일치하도록 시작 및 끝 위치를 업데이트하려면 라인 렌더러가 필요하다는 것입니다.
몇 가지 이유 때문에 내 라인 렌더러가 절대적으로 정적이어서 GameObject가 결국 이동하는 영역과 함께 시작 및 끝 위치를 조정할 수있는 방법을 찾을 수 없습니다.
여기 내 C# 스크립트, 내가 잘못하고있는 것에 대한 아이디어가 있습니까? 어떤 도움이라도 대단히 감사합니다! 당신이 새로운 변수 대신에 당신의 멤버 변수에 AddComponent
와 LineRenderer
을 할당하고 방법
using UnityEngine;
using System.Collections;
public class autoCreateNetwork : MonoBehaviour {
public bool sphereHasGravity = false;
private Collider coll;
private Renderer rend;
private SpringJoint springJoint;
public Material lines;
GameObject sphereObject0;
GameObject sphereObject2;
LineRenderer lineRenderer0_2 ;
void Start() {
// Spawn a sphere and set it up
sphereObject0 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphereObject0.transform.position = new Vector3(-10.2571400951f, 98.8977277884f, -19.9870244083f);
Rigidbody sphereBody0 = sphereObject0.AddComponent<Rigidbody>();
sphereBody0.mass = 1f;
// Physics model
coll = sphereBody0.GetComponent<Collider>();
PhysicMaterial material0 = new PhysicMaterial();
material0.dynamicFriction = 1;
coll.material = material0;
coll.material.bounciness = .8f;
// Render (color, smoothness, etc.)
rend = sphereBody0.GetComponent<Renderer>();
rend.material.color = new Color32(171,166,164,255);
rend.material.SetFloat("_Metallic", 0.0f);
rend.material.SetFloat("_Glossiness", 0.0f);
sphereBody0.useGravity = sphereHasGravity;
sphereBody0.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
// Spawn a sphere and set it up
sphereObject2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphereObject2.transform.position = new Vector3(-28.1943570987f, 89.5665878403f, 5.43686642264f);
Rigidbody sphereBody2 = sphereObject2.AddComponent<Rigidbody>();
sphereBody2.mass = 1f;
// Physics model
coll = sphereBody2.GetComponent<Collider>();
PhysicMaterial material2 = new PhysicMaterial();
material2.dynamicFriction = 1;
coll.material = material2;
coll.material.bounciness = .8f;
// Render (color, smoothness, etc.)
rend = sphereBody2.GetComponent<Renderer>();
rend.material.color = new Color32(105,150,187,255);
rend.material.SetFloat("_Metallic", 0.0f);
rend.material.SetFloat("_Glossiness", 0.0f);
sphereBody2.useGravity = sphereHasGravity;
sphereBody2.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
// Spawn a line and set it up
GameObject line0_2 = new GameObject();
LineRenderer lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();
lineRenderer0_2.material = lines;
lineRenderer0_2.widthMultiplier = 0.0482773661501f;
lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);
float alpha0_2 = .9f;
Gradient gradient0_2 = new Gradient();
gradient0_2.SetKeys(
new GradientColorKey[] { new GradientColorKey(new Color32(171,166,164,255), 0.0f), new GradientColorKey(new Color32(105,150,187,255), 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(alpha0_2, 0.0f), new GradientAlphaKey(alpha0_2, 1.0f) }
);
lineRenderer0_2.colorGradient = gradient0_2;
lineRenderer0_2.useWorldSpace = true;
}
void Update() {
lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);
}
}
환상적인 (정말 분명하다), Gunther에게 감사드립니다! – Rodolphe