如何在运行时更新线条渲染器的开始和结束位置?

时间:2017-03-24 18:57:43

标签: c# unity3d

我尝试在Unity3D项目中使用线渲染器 lineRenderer0_2 连接两个GameObject( sphereObject0 sphereObject2 )。< / p>

该行最初显示正常。我的问题是我需要线渲染器更新其开始和结束位置以匹配 sphereObject0 sphereObject2 的位置。

由于某些原因,我的线渲染器绝对是静态的,我无法找到方法来调整其开始和结束位置以及游戏对象最终移动的球体。

这是我的C#剧本,对我做错了什么的任何想法?非常感谢任何帮助!

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);

        }
    }

1 个答案:

答案 0 :(得分:1)

您的问题是如何将LineRenderer AddComponent分配给新变量而不是成员变量。这一行:

LineRenderer lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();

lineRenderer0_2范围内创建一个名为Start()的新变量,并在该函数的其他地方引用时隐藏成员变量。将行更改为:

lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();

它应该可以正常工作。作为一个侧节点,用&#34; m _&#34;来命名成员变量是很常见的。前缀,这样你就不会遇到这些问题。