AI Can Help Write C# Code for your Unity Game
Prompt: "Create a ShipRotator C# for unity that will simulate the gentle rolling of a TallShip prefab on the Z-Axis. I want to adjust the angle and time parameters in the unity editor, but set some sensible defaults to start with"
Results:
In order to create a ShipRotator script in C# for Unity, you can follow these steps: Open your Unity project and create a new C# script called "ShipRotator".
Replace the default content of the script with the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipRotator : MonoBehaviour
{
[SerializeField] private float rotationAngle = 4f; // Max rotation angle in degrees - adjust as needed
[SerializeField] private float rotationTime = 20f; // Time taken for the ship to reach max rotation - adjust as needed
private float currentRotation = 0f;
private float targetRotation;
private float initialRotation;
void Start()
{
initialRotation = transform.eulerAngles.z;
targetRotation = initialRotation + rotationAngle;
StartCoroutine(RotateShip());
}
IEnumerator RotateShip()
{
while (true)
{
float t = Mathf.PingPong(Time.time / rotationTime, 1);
currentRotation = Mathf.Lerp(initialRotation - rotationAngle, initialRotation + rotationAngle, t);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, currentRotation);
yield return null;
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class ShipRotator : MonoBehaviour
{
[SerializeField] private float rotationAngle = 4f; // Max rotation angle in degrees - adjust as needed
[SerializeField] private float rotationTime = 20f; // Time taken for the ship to reach max rotation - adjust as needed
private float currentRotation = 0f;
private float targetRotation;
private float initialRotation;
void Start()
{
initialRotation = transform.eulerAngles.z;
targetRotation = initialRotation + rotationAngle;
StartCoroutine(RotateShip());
}
IEnumerator RotateShip()
{
while (true)
{
float t = Mathf.PingPong(Time.time / rotationTime, 1);
currentRotation = Mathf.Lerp(initialRotation - rotationAngle, initialRotation + rotationAngle, t);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, currentRotation);
yield return null;
}
}
}
This script defines a ShipRotator class with two serialized fields, rotationAngle and rotationTime, which can be adjusted in the Unity editor. It sets sensible defaults for both parameters: 10 degrees for the rotation angle and 2 seconds for the rotation time. The RotateShip() coroutine is responsible for gently rolling the ship on the Z-axis using Mathf.Lerp and Mathf.PingPong.
Save the script and return to the Unity editor.
Attach the ShipRotator script to your TallShip prefab by selecting it in the hierarchy, clicking on "Add Component" in the inspector, and searching for "ShipRotator".
Adjust the rotationAngle and rotationTime parameters in the Unity editor if needed.
Now, when you run your Unity project, the TallShip prefab should gently roll on the Z-axis. You can modify the rotation angle and time parameters in the Unity editor to achieve the desired effect.
Note: If you want to play the prototype game, just ask.
Comments
Post a Comment