private void GeneratePiece() { Vector2 direction; float velocity; PieceParameters parameters; direction = new Vector2(UnityEngine.Random.Range(-0.5f, 0.5f), 1.0f); direction = direction.normalized; velocity = UnityEngine.Random.Range(5.0f, 20.0f) * 100.0f; parameters = new PieceParameters { Direction = direction, Velocity = velocity, Properties = new PieceProperties { Brand = (PieceBrand)UnityEngine.Random.Range(1, 4), Color = (PieceColor)UnityEngine.Random.Range(1, 4), Shape = (PieceShape)UnityEngine.Random.Range(1, 4), Size = (PieceSize)UnityEngine.Random.Range(1, 4) } }; OnPieceReceived?.Invoke(parameters); }
private void PieceListener_OnPieceReceived(PieceParameters parameters) { GameObject pieceClone; PieceComp pieceParameters; Rigidbody body; Vector3 randomPos; Quaternion quat; if (!_canThrow) { return; } quat = Quaternion.FromToRotation(Vector3.forward, transform.forward); pieceClone = Instantiate(pieceObject); pieceClone.SetActive(true); randomPos = new Vector3( Mathf.Cos(UnityEngine.Random.Range(-180.0f * Mathf.Deg2Rad, 180.0f * Mathf.Deg2Rad)), Mathf.Sin(UnityEngine.Random.Range(-180.0f * Mathf.Deg2Rad, 180.0f * Mathf.Deg2Rad)), 0); randomPos = randomPos.normalized; randomPos = quat * randomPos; pieceParameters = pieceClone.GetComponent <PieceComp>(); pieceParameters.SetParameters(parameters); pieceClone.transform.position = transform.position + randomPos * spawnRadius; pieceClone.transform.rotation = transform.rotation; body = pieceClone.GetComponent <Rigidbody>(); body.AddForce(quat * new Vector3(parameters.Direction.x, 0, parameters.Direction.y) * parameters.Velocity); }
public void Initialize(PieceParameters parameters) { color = parameters.Properties.Color ?? PieceColor.Undefined; brand = parameters.Properties.Brand ?? PieceBrand.Undefined; shape = parameters.Properties.Shape ?? PieceShape.Undefined; size = parameters.Properties.Size ?? PieceSize.Undefined; direction = parameters.Direction; velocity = parameters.Velocity; }
public void SetParameters(PieceParameters parameters) { color = parameters.Properties.Color ?? PieceColor.Undefined; brand = parameters.Properties.Brand ?? PieceBrand.Undefined; shape = parameters.Properties.Shape ?? PieceShape.Undefined; size = parameters.Properties.Size ?? PieceSize.Undefined; //Change color randomly switch (color) { case PieceColor.Red: myLight.color = Color.red; break; case PieceColor.Green: myLight.color = Color.green; break; case PieceColor.Blue: myLight.color = Color.blue; break; default: myLight.color = Color.black; break; } shapeMesh = GetComponent <MeshFilter>(); switch (shape) { case PieceShape.Cube: shapeMesh.mesh = meshes[0]; break; case PieceShape.Sphere: shapeMesh.mesh = meshes[1]; break; case PieceShape.Cylinder: shapeMesh.mesh = meshes[2]; break; default: shapeMesh.mesh = meshes[0]; break; } myMat = GetComponent <Renderer>(); switch (brand) { case PieceBrand.Durex: myMat.material.SetTexture("_MainTex", textures[0]); break; case PieceBrand.Lays: myMat.material.SetTexture("_MainTex", textures[1]); break; case PieceBrand.Ikea: myMat.material.SetTexture("_MainTex", textures[2]); break; default: myMat.material.SetTexture("_MainTex", textures[0]); break; } switch (size) { case PieceSize.Small: transform.localScale = new Vector3(0.2f, 0.2f, 0.2f); break; case PieceSize.Medium: transform.localScale = new Vector3(0.42f, 0.42f, 0.42f); break; case PieceSize.Large: transform.localScale = new Vector3(0.65f, 0.65f, 0.65f); break; default: transform.localScale = new Vector3(1f, 1f, 1f); break; } }