Spaces:
Sleeping
Sleeping
File size: 1,594 Bytes
defab59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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);
}
}
}
|