SWS-Smarta / project /Assets /Scripts /Game /MovingObstacle.cs
alfapro's picture
Clean deploy: Code only for SWS
defab59
Raw History Blame Contribute Delete
1.59 kB
using Fusion;
using UnityEngine;
public class MovingObstacle : NetworkBehaviour
{
public enum MoveType { Horizontal, Vertical, Swing, Rotate }
public MoveType moveType = MoveType.Horizontal;
public float amplitude = 1.2f;
public float speed = 1.5f;
public Vector3 axis = Vector3.right;
public float rotateAngle = 45f;
Vector3 startPos;
Quaternion startRot;
double startTime;
public override void Spawned()
{
startPos = transform.position;
startRot = transform.rotation;
if (Object.HasStateAuthority)
startTime = Runner.SimulationTime;
}
public override void FixedUpdateNetwork()
{
if (!Object.HasStateAuthority)
return;
float t = (float)(Runner.SimulationTime - startTime);
float s = Mathf.Sin(t * speed);
if (moveType == MoveType.Horizontal)
{
Vector3 pos = startPos + new Vector3(s * amplitude, 0f, 0f);
transform.position = pos;
}
else if (moveType == MoveType.Vertical)
{
Vector3 pos = startPos + new Vector3(0f, s * amplitude, 0f);
transform.position = pos;
}
else if (moveType == MoveType.Swing)
{
float angle = s * rotateAngle;
transform.rotation = startRot * Quaternion.AngleAxis(angle, axis.normalized);
}
else if (moveType == MoveType.Rotate)
{
transform.rotation = transform.rotation * Quaternion.AngleAxis(speed * Runner.DeltaTime * 360f, axis.normalized);
}
}
}