In: Computer Science
Add a sphere into Unity. write ONE C# script in Unity that does the following:
Set the sphere to a color of your choice.
Start with growing the sphere’s scale at 0.0005/frame until its scale reaches 3, then shrink
the sphere’s scale at the same rate until it reaches 1. Repeat.
Sphere rotate around the origin in XY plane at radius of 5.
position = transform.localPosition;
position.x = radius * Mathf.Sin(Time.fixedTime);
position.y = radius * Mathf.Cos(Time.fixedTime);
Answer:
//SphereBehaviour.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereBehaviour : MonoBehaviour
{
private float scale = 1.0f;
private float radius = 5.0f;
// Start is called before the first frame
update
void Start()
{
//sets the color of the
sphere in start()
gameObject.GetComponent<Renderer>().material.color =
Color.red;
}
// Update is called once per frame
void Update()
{
Rotate();
if (scale >= 0
&& scale <= 3)
{
scale += 0.0005f;
transform.localScale += new Vector3(0.0005f, 0.0005f,
0.0005f);
}
else if (scale <
0)
{
scale += 0.0005f;
transform.localScale -= new Vector3(0.0005f, 0.0005f,
0.0005f);
}
else
{
scale = -3;
}
}
//rotates the sphere around the origin in xy
plane
void Rotate()
{
Vector3 position =
transform.localPosition;
position.x = radius * Mathf.Sin(Time.fixedTime);
position.y = radius * Mathf.Cos(Time.fixedTime);
transform.localPosition = position;
}
}