当我为主要预制件创建新的克隆时,该克隆应该具有相同的动作。
在我的代码中,克隆始终具有相同的动作。当我按m时,所有预制件的运动都应该改变。当我第一次按下动作brownianMotion2D()
时,当我现在创建预制件的新副本时,克隆预制件以动作circularMotion2D
()开始,但应该从其他动作开始。我怎样才能做到这一点?
该动作应与主要的预制件相同,当我更改为新的动作并创建克隆时。
globalcontrol.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class globalControl : MonoBehaviour
{
public GameObject prefabInstance;
List<Object> prefabInstanceClones = new List<Object>();
GameObject capsule, sphere, cylinder;
string color;
private bool mode = true;
private void Start()
{
capsule = GameObject.Find("Capsule");
sphere = GameObject.Find("Sphere");
cylinder = GameObject.Find("Cylinder");
}
public void SetMode(bool isOn)
{
mode = isOn;
}
void Update()
{
Debug.Log(prefabInstanceClones.Count);
if (Input.GetKeyDown("c"))
{
prefabInstanceClones.Add(Instantiate(prefabInstance, transform.position, Quaternion.identity));
//GameObject newClone = Instantiate(prefabInstance, transform.position, Quaternion.identity) as GameObject;
//newClone.GetComponent<player>();
//prefabInstanceClones.Add(newClone);
}
if (Input.GetKeyDown("d"))
{
if (prefabInstanceClones.Count >= 1) {
var last = prefabInstanceClones[prefabInstanceClones.Count - 1];
prefabInstanceClones.Remove(last);
Destroy(last);
}
}
if (Input.GetKeyDown("r"))
{
capsule.GetComponent<Renderer>().material.color = Color.red;
sphere.GetComponent<Renderer>().material.color = Color.red;
cylinder.GetComponent<Renderer>().material.color = Color.red;
color = "red";
}
if (Input.GetKeyDown("b"))
{
capsule.GetComponent<Renderer>().material.color = Color.blue;
sphere.GetComponent<Renderer>().material.color = Color.blue;
cylinder.GetComponent<Renderer>().material.color = Color.blue;
color = "blue";
}
if (Input.GetKeyDown("g"))
{
capsule.GetComponent<Renderer>().material.color = Color.green;
sphere.GetComponent<Renderer>().material.color = Color.green;
cylinder.GetComponent<Renderer>().material.color = Color.green;
color = "green";
}
if(color == "red")
{
colorChanger(Color.red);
}
if (color == "blue")
{
colorChanger(Color.blue);
}
if (color == "green")
{
colorChanger(Color.green);
}
}
/// <summary>
/// Change the color of the prefab
/// </summary>
/// <param name="pColor"> the color to be changed to</param>
void colorChanger(Color pColor) {
foreach (GameObject clone in prefabInstanceClones)
{
Component[] colorElements = clone.GetComponentsInChildren<Renderer>();
foreach (Renderer color in colorElements)
{
color.material.color = pColor;
}
}
}
}
player.cs
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
public class player : MonoBehaviour
{
private int _speed;
private int _radius;
float counter = 0;
private float x;
private float y;
private float posX;
private float posY;
bool mode = true;
bool first = true;
public void f_init(float x, float y)
{
_speed = (int)x;
_radius = (int)y;
}
public void circularMotion2D()
{
posX = Camera.main.transform.position.x;
posY = Camera.main.transform.position.y;
counter = counter + _speed;
x = Mathf.Cos(counter/60) * _radius + posX;
y = Mathf.Sin(counter/60) * _radius + posY;
transform.position = new Vector2(x, y);
}
public void brownianMotion2D()
{
x = x + Random.Range(-0.2f, 0.2f) + _speed/60;
y = y + Random.Range(-0.2f, 0.2f) + _speed/60;
transform.position = new Vector2(x, y);
}
// Start is called before the first frame update
void Start()
{
f_init(1f, 3f);
}
// Update is called once per frame
void Update()
{
if (mode == false)
{
brownianMotion2D();
}
else
{
circularMotion2D();
}
if (Input.GetKeyDown("m")) {
if (first == true)
{
mode = false;
first = false;
}
else {
mode = true;
first = true;
}
}
}
}