Skip to content

Instantly share code, notes, and snippets.

@Narazaka
Created March 12, 2025 11:08
Show Gist options
  • Save Narazaka/e88d05774eaf9e41f27123d5f8119a25 to your computer and use it in GitHub Desktop.
Save Narazaka/e88d05774eaf9e41f27123d5f8119a25 to your computer and use it in GitHub Desktop.
ErrinFlyer 1号機 の飛行コード
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common;
public class FlyPlane : UdonSharpBehaviour
{
[SerializeField]
Animator[] Propellers;
[SerializeField]
float SpeedBase = 125f;
[SerializeField]
float FlyBase = 110f;
[SerializeField]
float DirectionBase = 4f;
[SerializeField]
float FixRotXLimit;
[SerializeField]
float FixRotX;
[SerializeField]
float FixRotZLimit;
[SerializeField]
float FixRotZ;
[UdonSynced(UdonSyncMode.Smooth)]
public float Speed;
[UdonSynced(UdonSyncMode.Smooth)]
public float Direction;
public bool Active;
float InputSpeed;
float InputDirection;
const float MaxSpeed = 20;
Rigidbody Rigidbody;
void Start()
{
Rigidbody = GetComponent<Rigidbody>();
}
public override void InputMoveHorizontal(float value, UdonInputEventArgs args)
{
InputDirection = value;
}
public override void InputMoveVertical(float value, UdonInputEventArgs args)
{
InputSpeed = value;
}
void Update()
{
var len = Propellers.Length;
for (int i = 0; i < len; i++)
{
Propellers[i].SetFloat("speed", Speed);
}
if (!Networking.IsOwner(gameObject)) return;
if (Active)
{
Direction += InputDirection * Time.deltaTime;
Direction += (Direction == 0 ? 0 : Direction > 0 ? -1 : 1) * 0.5f * Time.deltaTime;
if (Direction < -1) Direction = -1;
if (Direction > 1) Direction = 1;
// Speed -= 10f * Time.deltaTime;
Speed += InputSpeed * 0.5f;
if (Speed > MaxSpeed) Speed = MaxSpeed;
if (Speed < 0) Speed = 0;
}
var sqrV = Rigidbody.velocity.sqrMagnitude;
Rigidbody.AddRelativeForce(new Vector3(0, sqrV > 16 ? Speed * FlyBase : 0f, Speed * SpeedBase));
if (sqrV < 4) return;
transform.localRotation *= Quaternion.Euler(0, Direction * DirectionBase * Time.deltaTime, 0f);
var eulerAngles = transform.localRotation.eulerAngles;
var rotX = eulerAngles.x;
if (rotX > 180) rotX -= 360;
if (rotX < -FixRotXLimit)
{
transform.localRotation *= Quaternion.Euler(FixRotX * Time.deltaTime, 0, 0);
Debug.Log($"{rotX} => {transform.localRotation.eulerAngles.x}");
}
else if (rotX > FixRotXLimit)
{
transform.localRotation *= Quaternion.Euler(-FixRotX * Time.deltaTime, 0, 0);
Debug.Log($"{rotX} => {transform.localRotation.eulerAngles.x}");
}
var rotZ = eulerAngles.z;
if (rotZ > 180) rotZ -= 360;
if (rotZ < -FixRotZLimit)
{
transform.localRotation *= Quaternion.Euler(0, 0, FixRotZ * Time.deltaTime);
}
else if (rotZ > FixRotZLimit)
{
transform.localRotation *= Quaternion.Euler(0, 0, -FixRotZ * Time.deltaTime);
}
else
{
transform.localRotation *= Quaternion.Euler(0, 0, Direction * Time.deltaTime);
}
}
}
@Narazaka
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment