Unity移动实例化对象

时间:2018-07-19 07:09:48

标签: c# arrays unity3d instantiation gameobject

下面的代码可以正常工作,除了实例化对象自己移动。我希望实例化的对象以0.5f的速度在pointA和pointB之间来回移动。

注意:由于此文件已附加到相机,因此我不尝试在Start()和Update()中使用注释的代码。

使用当前代码:我的objectList对象正在按预期方式移动,而不仅仅是其实例化对象。我希望实例化的对象像乒乓球一样通过偏移

移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsSpawner : MonoBehaviour {

public GameObject[] objectList;
public  GameObject[] objectSpawns;
int val;

public float speed = 0.5f;
Vector3 pointA;
Vector3 pointB;

// Use this for initialization
void Start () {
    val = PlayerPrefs.GetInt("CannonPowerVal");
    addToList();
    for (int i = 1; i < objectSpawns.Length; i++){
        pointA = new Vector3(-3.8f, objectSpawns[i].transform.localPosition.y, 0);
        pointB = new Vector3(3.8f, objectSpawns[i].transform.localPosition.y, 0);
    }

    //pointA = new Vector3(-3.8f, transform.localPosition.y, 0);
    //pointB = new Vector3(3.8f, transform.localPosition.y, 0);

}

// Update is called once per frame
void Update()
{
    //PingPong between 0 and 1
    float time = Mathf.PingPong(Time.time * speed, 1);
    //transform.position = Vector3.Lerp(pointA, pointB, time);

    for (int i = 1; i < objectSpawns.Length; i++)
    {
        objectSpawns[i].transform.position = Vector3.Lerp(pointA, pointB, time);
    }
}

public void addToList(){
    objectSpawns = new GameObject[val];
    int max = objectList.Length;
    int counter = 8; // set first object out of screen sight

    // Adds Scene Objects from objectList to objectSpawns
    // Size of SceneObjects determined by CannonPowerVal

    for (int i = 0; i < PlayerPrefs.GetInt("CannonPowerVal"); i++){

        objectSpawns.SetValue(objectList[Random.Range(0, max)], i);
        // Random x spawn(-2.8f, 2.8f)
        Instantiate(objectSpawns[i], new Vector2(transform.localPosition.x + Random.Range(-2.8f,2.8f), transform.localPosition.y + counter), Quaternion.identity);

        counter = counter + 5;
    }
}

}

1 个答案:

答案 0 :(得分:2)

实例化对象后,您忘记将引用添加到brew install mysql-connector-c brew install mysql brew link --overwrite mysql pip install MySQL-python 的列表中。

objectSpawns方法中执行以下操作:

addToList()