namespacify everything
This commit is contained in:
@@ -1,99 +1,98 @@
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
namespace Game;
|
||||
|
||||
public class CameraMovement : Script
|
||||
{
|
||||
public class CameraMovement : Script
|
||||
private float viewPitch;
|
||||
private float viewRoll;
|
||||
private float viewYaw;
|
||||
|
||||
[Limit(0, 9000)]
|
||||
[Tooltip("Camera speed")]
|
||||
public float MoveSpeed { get; set; } = 400;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
private float viewPitch;
|
||||
private float viewRoll;
|
||||
private float viewYaw;
|
||||
Float3 initialEulerAngles = Actor.Orientation.EulerAngles;
|
||||
viewPitch = initialEulerAngles.X;
|
||||
viewYaw = initialEulerAngles.Y;
|
||||
viewRoll = initialEulerAngles.Z;
|
||||
}
|
||||
|
||||
[Limit(0, 9000)]
|
||||
[Tooltip("Camera speed")]
|
||||
public float MoveSpeed { get; set; } = 400;
|
||||
public override void OnUpdate()
|
||||
{
|
||||
Transform camTrans = Actor.Transform;
|
||||
Actor rootActor = Actor.GetChild(0);
|
||||
Camera camera = rootActor.GetChild<Camera>();
|
||||
|
||||
public override void OnStart()
|
||||
float xAxis = InputManager.GetAxisRaw("Mouse X");
|
||||
float yAxis = InputManager.GetAxisRaw("Mouse Y");
|
||||
if (xAxis != 0.0f || yAxis != 0.0f)
|
||||
{
|
||||
Float3 initialEulerAngles = Actor.Orientation.EulerAngles;
|
||||
viewPitch = initialEulerAngles.X;
|
||||
viewYaw = initialEulerAngles.Y;
|
||||
viewRoll = initialEulerAngles.Z;
|
||||
viewPitch += yAxis;
|
||||
viewYaw += xAxis;
|
||||
|
||||
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
|
||||
|
||||
// root orientation must be set first
|
||||
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
|
||||
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
float inputH = InputManager.GetAxis("Horizontal");
|
||||
float inputV = InputManager.GetAxis("Vertical");
|
||||
Float3 move = new Float3(inputH, 0.0f, inputV);
|
||||
|
||||
if (!move.IsZero)
|
||||
{
|
||||
Transform camTrans = Actor.Transform;
|
||||
Actor rootActor = Actor.GetChild(0);
|
||||
Camera camera = rootActor.GetChild<Camera>();
|
||||
move.Normalize();
|
||||
move = camera.Transform.TransformDirection(move) * MoveSpeed;
|
||||
|
||||
float xAxis = InputManager.GetAxisRaw("Mouse X");
|
||||
float yAxis = InputManager.GetAxisRaw("Mouse Y");
|
||||
if (xAxis != 0.0f || yAxis != 0.0f)
|
||||
{
|
||||
viewPitch += yAxis;
|
||||
viewYaw += xAxis;
|
||||
Float3 delta = move * Time.UnscaledDeltaTime;
|
||||
float movementLeft = delta.Length;
|
||||
|
||||
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
|
||||
// TODO: check multiple times in case we get stuck in walls
|
||||
|
||||
// root orientation must be set first
|
||||
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
|
||||
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
|
||||
}
|
||||
|
||||
float inputH = InputManager.GetAxis("Horizontal");
|
||||
float inputV = InputManager.GetAxis("Vertical");
|
||||
Float3 move = new Float3(inputH, 0.0f, inputV);
|
||||
|
||||
if (!move.IsZero)
|
||||
{
|
||||
move.Normalize();
|
||||
move = camera.Transform.TransformDirection(move) * MoveSpeed;
|
||||
float sphereRadius = 10.0f; // TODO: use collider radius
|
||||
RayCastHit[] hitInfos;
|
||||
float moveDist = delta.Length;
|
||||
Physics.SphereCastAll(Actor.Transform.Translation, sphereRadius, move.Normalized, out hitInfos,
|
||||
moveDist);
|
||||
|
||||
//bool nohit = true;
|
||||
float hitDistance = moveDist;
|
||||
Float3 hitNormal = move.Normalized;
|
||||
foreach (RayCastHit hitInfo in hitInfos)
|
||||
{
|
||||
Float3 delta = move * Time.UnscaledDeltaTime;
|
||||
float movementLeft = delta.Length;
|
||||
if (hitInfo.Collider.Parent == Parent)
|
||||
continue;
|
||||
|
||||
// TODO: check multiple times in case we get stuck in walls
|
||||
|
||||
float sphereRadius = 10.0f; // TODO: use collider radius
|
||||
RayCastHit[] hitInfos;
|
||||
float moveDist = delta.Length;
|
||||
Physics.SphereCastAll(Actor.Transform.Translation, sphereRadius, move.Normalized, out hitInfos,
|
||||
moveDist);
|
||||
|
||||
//bool nohit = true;
|
||||
float hitDistance = moveDist;
|
||||
Float3 hitNormal = move.Normalized;
|
||||
foreach (RayCastHit hitInfo in hitInfos)
|
||||
if (hitInfo.Distance < hitDistance)
|
||||
{
|
||||
if (hitInfo.Collider.Parent == Parent)
|
||||
continue;
|
||||
|
||||
if (hitInfo.Distance < hitDistance)
|
||||
{
|
||||
hitDistance = hitInfo.Distance;
|
||||
hitNormal = hitInfo.Normal;
|
||||
}
|
||||
//nohit = false;
|
||||
//break;
|
||||
hitDistance = hitInfo.Distance;
|
||||
hitNormal = hitInfo.Normal;
|
||||
}
|
||||
|
||||
if (hitDistance != moveDist)
|
||||
//camTrans.Translation = Float3.Lerp(Actor.Transform.Translation, camTrans.Translation, hitDistance);
|
||||
|
||||
//projected = normal * dot(direction, normal);
|
||||
//direction = direction - projected
|
||||
|
||||
//camTrans.Translation += hitNormal * (moveDist - hitDistance); // correct?
|
||||
//camTrans.Translation = hitNormal * (move * hitNormal); // correct?
|
||||
//camTrans.Translation = Actor.Transform.Translation;
|
||||
delta += -Float3.Dot(delta, hitNormal) * hitNormal; // correct?
|
||||
|
||||
camTrans.Translation += delta;
|
||||
//nohit = false;
|
||||
//break;
|
||||
}
|
||||
}
|
||||
|
||||
Actor.Transform = camTrans;
|
||||
if (hitDistance != moveDist)
|
||||
//camTrans.Translation = Float3.Lerp(Actor.Transform.Translation, camTrans.Translation, hitDistance);
|
||||
|
||||
//projected = normal * dot(direction, normal);
|
||||
//direction = direction - projected
|
||||
|
||||
//camTrans.Translation += hitNormal * (moveDist - hitDistance); // correct?
|
||||
//camTrans.Translation = hitNormal * (move * hitNormal); // correct?
|
||||
//camTrans.Translation = Actor.Transform.Translation;
|
||||
delta += -Float3.Dot(delta, hitNormal) * hitNormal; // correct?
|
||||
|
||||
camTrans.Translation += delta;
|
||||
}
|
||||
}
|
||||
|
||||
Actor.Transform = camTrans;
|
||||
}
|
||||
}
|
||||
@@ -1,182 +1,181 @@
|
||||
using FlaxEditor.Content.Settings;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
namespace Game;
|
||||
|
||||
/// <summary>
|
||||
/// CameraRender Script.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
public class CameraRender : PostProcessEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// CameraRender Script.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
public class CameraRender : PostProcessEffect
|
||||
public Camera camera;
|
||||
|
||||
private bool lastEnabled;
|
||||
public Material material;
|
||||
|
||||
private MaterialInstance materialInstance;
|
||||
private SceneRenderTask sceneTask;
|
||||
private SceneRenderTask sceneTask2;
|
||||
|
||||
private GPUTexture texture;
|
||||
private GPUTexture texture2;
|
||||
|
||||
//public override PostProcessEffectLocation Location => PostProcessEffectLocation.Default;
|
||||
//public override int Order => 110;
|
||||
//public override bool CanRender => camera.IsActive;
|
||||
|
||||
private bool useMainCamera = true;
|
||||
public bool rescaleModel = true;
|
||||
|
||||
private Actor viewModelHolder;
|
||||
|
||||
private void CreateTextures(int width, int height)
|
||||
{
|
||||
public Camera camera;
|
||||
GPUTextureDescription textureDesc = GPUTextureDescription.New2D(width, height, PixelFormat.R8G8B8A8_UNorm);
|
||||
|
||||
private bool lastEnabled;
|
||||
public Material material;
|
||||
// Prepare texture and SceneRenderTask for viewmodel camera
|
||||
if (texture == null)
|
||||
texture = new GPUTexture();
|
||||
if (texture.Init(ref textureDesc))
|
||||
Console.Print("Failed to create camera texture");
|
||||
|
||||
private MaterialInstance materialInstance;
|
||||
private SceneRenderTask sceneTask;
|
||||
private SceneRenderTask sceneTask2;
|
||||
// Prepare depth texture and SceneRenderTask for viewmodel camera
|
||||
textureDesc.Format = PixelFormat.R8_UNorm;
|
||||
if (texture2 == null)
|
||||
texture2 = new GPUTexture();
|
||||
if (texture2.Init(ref textureDesc))
|
||||
Console.Print("Failed to create camera depth texture");
|
||||
}
|
||||
|
||||
private GPUTexture texture;
|
||||
private GPUTexture texture2;
|
||||
|
||||
//public override PostProcessEffectLocation Location => PostProcessEffectLocation.Default;
|
||||
//public override int Order => 110;
|
||||
//public override bool CanRender => camera.IsActive;
|
||||
|
||||
private bool useMainCamera = true;
|
||||
public bool rescaleModel = true;
|
||||
|
||||
private Actor viewModelHolder;
|
||||
|
||||
private void CreateTextures(int width, int height)
|
||||
public override void OnAwake()
|
||||
{
|
||||
viewModelHolder = Parent.Parent.Parent.Parent.GetChild("ViewModelHolder");
|
||||
if (useMainCamera)
|
||||
{
|
||||
GPUTextureDescription textureDesc = GPUTextureDescription.New2D(width, height, PixelFormat.R8G8B8A8_UNorm);
|
||||
|
||||
// Prepare texture and SceneRenderTask for viewmodel camera
|
||||
if (texture == null)
|
||||
texture = new GPUTexture();
|
||||
if (texture.Init(ref textureDesc))
|
||||
Console.Print("Failed to create camera texture");
|
||||
|
||||
// Prepare depth texture and SceneRenderTask for viewmodel camera
|
||||
textureDesc.Format = PixelFormat.R8_UNorm;
|
||||
if (texture2 == null)
|
||||
texture2 = new GPUTexture();
|
||||
if (texture2.Init(ref textureDesc))
|
||||
Console.Print("Failed to create camera depth texture");
|
||||
}
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
viewModelHolder = Parent.Parent.Parent.Parent.GetChild("ViewModelHolder");
|
||||
if (useMainCamera)
|
||||
camera.IsActive = false;
|
||||
void foo(Actor actor)
|
||||
{
|
||||
camera.IsActive = false;
|
||||
void foo(Actor actor)
|
||||
{
|
||||
actor.Layer = 0;
|
||||
foreach (Actor actChild in actor.GetChildren<Actor>())
|
||||
foo(actChild);
|
||||
}
|
||||
|
||||
foo(viewModelHolder);
|
||||
actor.Layer = 0;
|
||||
foreach (Actor actChild in actor.GetChildren<Actor>())
|
||||
foo(actChild);
|
||||
}
|
||||
|
||||
if (!camera.IsActive)
|
||||
return;
|
||||
|
||||
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
|
||||
|
||||
// Color pass
|
||||
sceneTask = new SceneRenderTask();
|
||||
sceneTask.Order = -1;
|
||||
sceneTask.Camera = camera;
|
||||
sceneTask.ViewMode = ViewMode.Default;
|
||||
sceneTask.Output = texture;
|
||||
sceneTask.ViewFlags = ViewFlags.DefaultGame;
|
||||
sceneTask.Enabled = true;
|
||||
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
|
||||
|
||||
// Depth pass
|
||||
sceneTask2 = new SceneRenderTask();
|
||||
sceneTask2.Order = -2;
|
||||
sceneTask2.Camera = camera;
|
||||
sceneTask2.ViewMode = ViewMode.Depth;
|
||||
sceneTask2.Output = texture2;
|
||||
sceneTask2.ViewFlags = ViewFlags.None;
|
||||
sceneTask2.Enabled = true;
|
||||
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
|
||||
|
||||
// Setup material instance and parameters
|
||||
if (materialInstance == null)
|
||||
materialInstance = material.CreateVirtualInstance();
|
||||
materialInstance.SetParameterValue("Input", texture);
|
||||
materialInstance.SetParameterValue("Depth", texture2);
|
||||
|
||||
materialInstance.SetParameterValue("New parameter", true);
|
||||
materialInstance.SetParameterValue("New parameter 0", ChannelMask.Blue);
|
||||
materialInstance.SetParameterValue("New parameter 1", new Color(0.67f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 3", 123f);
|
||||
materialInstance.SetParameterValue("New parameter 4", new Float2(1,2));
|
||||
materialInstance.SetParameterValue("New parameter 5", new Float3(1,2,3));
|
||||
materialInstance.SetParameterValue("New parameter 6", new Float4(1,2,3,4));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 8", 123);
|
||||
materialInstance.SetParameterValue("New parameter 9", new Matrix(0.666f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 11", new Quaternion(0.5f, 0.5f, 0.5f, 0.5f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 13", new Vector2(1,2));
|
||||
materialInstance.SetParameterValue("New parameter 14", new Vector3(1,2,3));
|
||||
materialInstance.SetParameterValue("New parameter 15", new Vector4(1,2,3,4));
|
||||
//materialInstance.SetParameterValue("New parameter 16", new Transform(new Vector3(1,2,3), new Quaternion(0.5f, 0.5f, 0.5f, 0.5f)));
|
||||
|
||||
lastEnabled = true;
|
||||
foo(viewModelHolder);
|
||||
}
|
||||
|
||||
public override void OnDestroy()
|
||||
{
|
||||
Destroy(ref sceneTask);
|
||||
Destroy(ref sceneTask2);
|
||||
Destroy(ref texture);
|
||||
Destroy(ref texture2);
|
||||
}
|
||||
if (!camera.IsActive)
|
||||
return;
|
||||
|
||||
public override void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input,
|
||||
GPUTexture output)
|
||||
{
|
||||
if (texture == null || texture2 == null)
|
||||
return;
|
||||
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
|
||||
|
||||
Renderer.DrawPostFxMaterial(context, ref renderContext, materialInstance, output, input.View());
|
||||
}
|
||||
// Color pass
|
||||
sceneTask = new SceneRenderTask();
|
||||
sceneTask.Order = -1;
|
||||
sceneTask.Camera = camera;
|
||||
sceneTask.ViewMode = ViewMode.Default;
|
||||
sceneTask.Output = texture;
|
||||
sceneTask.ViewFlags = ViewFlags.DefaultGame;
|
||||
sceneTask.Enabled = true;
|
||||
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
|
||||
|
||||
private bool lastRescale = false;
|
||||
public override void OnUpdate()
|
||||
{
|
||||
// Depth pass
|
||||
sceneTask2 = new SceneRenderTask();
|
||||
sceneTask2.Order = -2;
|
||||
sceneTask2.Camera = camera;
|
||||
sceneTask2.ViewMode = ViewMode.Depth;
|
||||
sceneTask2.Output = texture2;
|
||||
sceneTask2.ViewFlags = ViewFlags.None;
|
||||
sceneTask2.Enabled = true;
|
||||
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
|
||||
|
||||
// Setup material instance and parameters
|
||||
if (materialInstance == null)
|
||||
materialInstance = material.CreateVirtualInstance();
|
||||
materialInstance.SetParameterValue("Input", texture);
|
||||
materialInstance.SetParameterValue("Depth", texture2);
|
||||
|
||||
materialInstance.SetParameterValue("New parameter", true);
|
||||
materialInstance.SetParameterValue("New parameter 0", ChannelMask.Blue);
|
||||
materialInstance.SetParameterValue("New parameter 1", new Color(0.67f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 3", 123f);
|
||||
materialInstance.SetParameterValue("New parameter 4", new Float2(1,2));
|
||||
materialInstance.SetParameterValue("New parameter 5", new Float3(1,2,3));
|
||||
materialInstance.SetParameterValue("New parameter 6", new Float4(1,2,3,4));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 8", 123);
|
||||
materialInstance.SetParameterValue("New parameter 9", new Matrix(0.666f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 11", new Quaternion(0.5f, 0.5f, 0.5f, 0.5f));
|
||||
|
||||
materialInstance.SetParameterValue("New parameter 13", new Vector2(1,2));
|
||||
materialInstance.SetParameterValue("New parameter 14", new Vector3(1,2,3));
|
||||
materialInstance.SetParameterValue("New parameter 15", new Vector4(1,2,3,4));
|
||||
//materialInstance.SetParameterValue("New parameter 16", new Transform(new Vector3(1,2,3), new Quaternion(0.5f, 0.5f, 0.5f, 0.5f)));
|
||||
|
||||
lastEnabled = true;
|
||||
}
|
||||
|
||||
public override void OnDestroy()
|
||||
{
|
||||
Destroy(ref sceneTask);
|
||||
Destroy(ref sceneTask2);
|
||||
Destroy(ref texture);
|
||||
Destroy(ref texture2);
|
||||
}
|
||||
|
||||
public override void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input,
|
||||
GPUTexture output)
|
||||
{
|
||||
if (texture == null || texture2 == null)
|
||||
return;
|
||||
|
||||
Renderer.DrawPostFxMaterial(context, ref renderContext, materialInstance, output, input.View());
|
||||
}
|
||||
|
||||
private bool lastRescale = false;
|
||||
public override void OnUpdate()
|
||||
{
|
||||
#if FLAX_EDITOR
|
||||
if (Input.GetKeyDown(KeyboardKeys.F7))
|
||||
{
|
||||
PhysicsSettings physicsSettings = GameSettings.Load<PhysicsSettings>();
|
||||
physicsSettings.EnableSubstepping = !physicsSettings.EnableSubstepping;
|
||||
GameSettings.Save(physicsSettings);
|
||||
//GameSettings.Apply();
|
||||
}
|
||||
if (Input.GetKeyDown(KeyboardKeys.F7))
|
||||
{
|
||||
PhysicsSettings physicsSettings = GameSettings.Load<PhysicsSettings>();
|
||||
physicsSettings.EnableSubstepping = !physicsSettings.EnableSubstepping;
|
||||
GameSettings.Save(physicsSettings);
|
||||
//GameSettings.Apply();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (lastEnabled != camera.IsActive)
|
||||
{
|
||||
lastEnabled = camera.IsActive;
|
||||
sceneTask.Enabled = lastEnabled;
|
||||
sceneTask2.Enabled = lastEnabled;
|
||||
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
|
||||
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
|
||||
}
|
||||
|
||||
if (useMainCamera && rescaleModel != lastRescale)
|
||||
{
|
||||
lastRescale = rescaleModel;
|
||||
if (rescaleModel)
|
||||
viewModelHolder.Scale = new Float3(0.75f);
|
||||
else
|
||||
viewModelHolder.Scale = new Float3(1.0f);
|
||||
}
|
||||
|
||||
if (!camera.IsActive)
|
||||
return;
|
||||
if (texture == null)
|
||||
OnAwake();
|
||||
|
||||
if ((int)camera.Viewport.Width != texture.Width || (int)camera.Viewport.Height != texture.Height)
|
||||
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
|
||||
}
|
||||
|
||||
public override void OnEnable()
|
||||
if (lastEnabled != camera.IsActive)
|
||||
{
|
||||
//OnAwake();
|
||||
lastEnabled = camera.IsActive;
|
||||
sceneTask.Enabled = lastEnabled;
|
||||
sceneTask2.Enabled = lastEnabled;
|
||||
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
|
||||
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
|
||||
}
|
||||
|
||||
if (useMainCamera && rescaleModel != lastRescale)
|
||||
{
|
||||
lastRescale = rescaleModel;
|
||||
if (rescaleModel)
|
||||
viewModelHolder.Scale = new Float3(0.75f);
|
||||
else
|
||||
viewModelHolder.Scale = new Float3(1.0f);
|
||||
}
|
||||
|
||||
if (!camera.IsActive)
|
||||
return;
|
||||
if (texture == null)
|
||||
OnAwake();
|
||||
|
||||
if ((int)camera.Viewport.Width != texture.Width || (int)camera.Viewport.Height != texture.Height)
|
||||
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
|
||||
}
|
||||
|
||||
public override void OnEnable()
|
||||
{
|
||||
//OnAwake();
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,74 @@
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
namespace Game;
|
||||
|
||||
public class CameraSpring : Script
|
||||
{
|
||||
public class CameraSpring : Script
|
||||
private bool lastGround;
|
||||
private Float3 lastPosition;
|
||||
public float percY;
|
||||
|
||||
private Actor playerActor;
|
||||
private PlayerMovement playerMovement;
|
||||
|
||||
public float speed = 240f;
|
||||
private Float3 targetOffset;
|
||||
private Actor viewModelHolder;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
private bool lastGround;
|
||||
private Float3 lastPosition;
|
||||
public float percY;
|
||||
playerActor = Actor.Parent.Parent;
|
||||
playerMovement = playerActor.GetScript<PlayerMovement>();
|
||||
viewModelHolder = playerActor.GetChild("ViewModelHolder");
|
||||
|
||||
private Actor playerActor;
|
||||
private PlayerMovement playerMovement;
|
||||
lastGround = playerMovement.OnGround;
|
||||
targetOffset = Actor.LocalPosition;
|
||||
}
|
||||
|
||||
public float speed = 240f;
|
||||
private Float3 targetOffset;
|
||||
private Actor viewModelHolder;
|
||||
private void UpdatePosition(Float3 position)
|
||||
{
|
||||
Actor.Position = position;
|
||||
viewModelHolder.Position = position;
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
public override void OnUpdate()
|
||||
{
|
||||
Float3 position = Actor.Parent.Position + targetOffset;
|
||||
Float3 targetPosition = position;
|
||||
|
||||
if (playerMovement.OnGround)
|
||||
{
|
||||
playerActor = Actor.Parent.Parent;
|
||||
playerMovement = playerActor.GetScript<PlayerMovement>();
|
||||
viewModelHolder = playerActor.GetChild("ViewModelHolder");
|
||||
|
||||
lastGround = playerMovement.OnGround;
|
||||
targetOffset = Actor.LocalPosition;
|
||||
}
|
||||
|
||||
private void UpdatePosition(Float3 position)
|
||||
{
|
||||
Actor.Position = position;
|
||||
viewModelHolder.Position = position;
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
Float3 position = Actor.Parent.Position + targetOffset;
|
||||
Float3 targetPosition = position;
|
||||
|
||||
if (playerMovement.OnGround)
|
||||
float deltaY = position.Y - lastPosition.Y;
|
||||
//if (Mathf.Abs(deltaY) < 10f)
|
||||
if (deltaY > 0)
|
||||
{
|
||||
float deltaY = position.Y - lastPosition.Y;
|
||||
//if (Mathf.Abs(deltaY) < 10f)
|
||||
if (deltaY > 0)
|
||||
if (deltaY > 100f)
|
||||
{
|
||||
if (deltaY > 100f)
|
||||
{
|
||||
// Teleported, snap instantly
|
||||
UpdatePosition(position);
|
||||
}
|
||||
else
|
||||
{
|
||||
const float catchUpDistance = 10f;
|
||||
const float catchUpMinMultip = 0.25f;
|
||||
percY = Mathf.Abs(deltaY) / catchUpDistance;
|
||||
percY = Mathf.Min(1.0f, percY + catchUpMinMultip);
|
||||
percY *= percY;
|
||||
// Teleported, snap instantly
|
||||
UpdatePosition(position);
|
||||
}
|
||||
else
|
||||
{
|
||||
const float catchUpDistance = 10f;
|
||||
const float catchUpMinMultip = 0.25f;
|
||||
percY = Mathf.Abs(deltaY) / catchUpDistance;
|
||||
percY = Mathf.Min(1.0f, percY + catchUpMinMultip);
|
||||
percY *= percY;
|
||||
|
||||
float adjustSpeed = speed * Time.DeltaTime * percY;
|
||||
float adjustSpeed = speed * Time.DeltaTime * percY;
|
||||
|
||||
position.Y = lastPosition.Y; //-= deltaY;
|
||||
position.Y = Mathf.MoveTowards(position.Y, targetPosition.Y, adjustSpeed);
|
||||
UpdatePosition(position);
|
||||
}
|
||||
position.Y = lastPosition.Y; //-= deltaY;
|
||||
position.Y = Mathf.MoveTowards(position.Y, targetPosition.Y, adjustSpeed);
|
||||
UpdatePosition(position);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdatePosition(position);
|
||||
}
|
||||
|
||||
lastPosition = position;
|
||||
lastGround = playerMovement.OnGround;
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdatePosition(position);
|
||||
}
|
||||
|
||||
lastPosition = position;
|
||||
lastGround = playerMovement.OnGround;
|
||||
}
|
||||
}
|
||||
@@ -1,83 +1,82 @@
|
||||
using System;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
namespace Game;
|
||||
|
||||
public class WeaponSway : Script
|
||||
{
|
||||
public class WeaponSway : Script
|
||||
private Actor cameraHolder;
|
||||
|
||||
private Actor rootActor;
|
||||
public float swaySpeed = 3000f;
|
||||
|
||||
private float timeRemainder;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
private Actor cameraHolder;
|
||||
rootActor = Actor.Parent.GetChild("RootActor");
|
||||
cameraHolder = rootActor.GetChild("CameraHolder");
|
||||
Actor.LocalOrientation = GetRotation();
|
||||
}
|
||||
|
||||
private Actor rootActor;
|
||||
public float swaySpeed = 3000f;
|
||||
private Quaternion GetRotation()
|
||||
{
|
||||
Quaternion pitch = cameraHolder.LocalOrientation;
|
||||
Quaternion yawRoll = rootActor.LocalOrientation;
|
||||
return yawRoll * pitch;
|
||||
}
|
||||
|
||||
private float timeRemainder;
|
||||
public override void OnLateUpdate()
|
||||
{
|
||||
Quaternion rotation = GetRotation();
|
||||
|
||||
public override void OnStart()
|
||||
Float3 targetAngles = rotation.EulerAngles;
|
||||
Float3 angles = Actor.LocalOrientation.EulerAngles;
|
||||
|
||||
// Ensure the swaying is smooth when framerate fluctuates slightly
|
||||
float remaining = Time.DeltaTime + timeRemainder;
|
||||
const float minTime = 1f / 120f;
|
||||
do
|
||||
{
|
||||
rootActor = Actor.Parent.GetChild("RootActor");
|
||||
cameraHolder = rootActor.GetChild("CameraHolder");
|
||||
Actor.LocalOrientation = GetRotation();
|
||||
}
|
||||
float stepTime = Mathf.Min(Time.DeltaTime, minTime);
|
||||
remaining -= stepTime;
|
||||
float swaySpeedScaled = swaySpeed * stepTime;
|
||||
|
||||
private Quaternion GetRotation()
|
||||
{
|
||||
Quaternion pitch = cameraHolder.LocalOrientation;
|
||||
Quaternion yawRoll = rootActor.LocalOrientation;
|
||||
return yawRoll * pitch;
|
||||
}
|
||||
float deltaX = Mathf.DeltaAngle(angles.X, targetAngles.X);
|
||||
float deltaY = Mathf.DeltaAngle(angles.Y, targetAngles.Y);
|
||||
float deltaZ = Mathf.DeltaAngle(angles.Z, targetAngles.Z);
|
||||
|
||||
public override void OnLateUpdate()
|
||||
{
|
||||
Quaternion rotation = GetRotation();
|
||||
const float maxAngle = 30f;
|
||||
if (deltaX > maxAngle)
|
||||
angles.X -= maxAngle - deltaX;
|
||||
else if (deltaX < -maxAngle)
|
||||
angles.X += maxAngle + deltaX;
|
||||
if (deltaY > maxAngle)
|
||||
angles.Y -= maxAngle - deltaY;
|
||||
else if (deltaY < -maxAngle)
|
||||
angles.Y += maxAngle + deltaY;
|
||||
if (deltaZ > maxAngle)
|
||||
angles.Z -= maxAngle - deltaZ;
|
||||
else if (deltaZ < -maxAngle)
|
||||
angles.Z += maxAngle + deltaZ;
|
||||
|
||||
Float3 targetAngles = rotation.EulerAngles;
|
||||
Float3 angles = Actor.LocalOrientation.EulerAngles;
|
||||
float percX = Mathf.Abs(deltaX) / maxAngle;
|
||||
float percY = Mathf.Abs(deltaY) / maxAngle;
|
||||
float percZ = Mathf.Abs(deltaZ) / maxAngle;
|
||||
float minSpeed = swaySpeedScaled * 0.00001f * 0f;
|
||||
|
||||
// Ensure the swaying is smooth when framerate fluctuates slightly
|
||||
float remaining = Time.DeltaTime + timeRemainder;
|
||||
const float minTime = 1f / 120f;
|
||||
do
|
||||
{
|
||||
float stepTime = Mathf.Min(Time.DeltaTime, minTime);
|
||||
remaining -= stepTime;
|
||||
float swaySpeedScaled = swaySpeed * stepTime;
|
||||
Func<float, float> fun = f => Mathf.Pow(f, 1.3f);
|
||||
|
||||
float deltaX = Mathf.DeltaAngle(angles.X, targetAngles.X);
|
||||
float deltaY = Mathf.DeltaAngle(angles.Y, targetAngles.Y);
|
||||
float deltaZ = Mathf.DeltaAngle(angles.Z, targetAngles.Z);
|
||||
angles.X = Mathf.MoveTowardsAngle(angles.X, targetAngles.X,
|
||||
Math.Max(swaySpeedScaled * fun(percX), minSpeed));
|
||||
angles.Y = Mathf.MoveTowardsAngle(angles.Y, targetAngles.Y,
|
||||
Math.Max(swaySpeedScaled * fun(percY), minSpeed));
|
||||
angles.Z = Mathf.MoveTowardsAngle(angles.Z, targetAngles.Z,
|
||||
Math.Max(swaySpeedScaled * fun(percZ), minSpeed));
|
||||
} while (remaining > minTime);
|
||||
|
||||
const float maxAngle = 30f;
|
||||
if (deltaX > maxAngle)
|
||||
angles.X -= maxAngle - deltaX;
|
||||
else if (deltaX < -maxAngle)
|
||||
angles.X += maxAngle + deltaX;
|
||||
if (deltaY > maxAngle)
|
||||
angles.Y -= maxAngle - deltaY;
|
||||
else if (deltaY < -maxAngle)
|
||||
angles.Y += maxAngle + deltaY;
|
||||
if (deltaZ > maxAngle)
|
||||
angles.Z -= maxAngle - deltaZ;
|
||||
else if (deltaZ < -maxAngle)
|
||||
angles.Z += maxAngle + deltaZ;
|
||||
timeRemainder -= remaining;
|
||||
|
||||
float percX = Mathf.Abs(deltaX) / maxAngle;
|
||||
float percY = Mathf.Abs(deltaY) / maxAngle;
|
||||
float percZ = Mathf.Abs(deltaZ) / maxAngle;
|
||||
float minSpeed = swaySpeedScaled * 0.00001f * 0f;
|
||||
|
||||
Func<float, float> fun = f => Mathf.Pow(f, 1.3f);
|
||||
|
||||
angles.X = Mathf.MoveTowardsAngle(angles.X, targetAngles.X,
|
||||
Math.Max(swaySpeedScaled * fun(percX), minSpeed));
|
||||
angles.Y = Mathf.MoveTowardsAngle(angles.Y, targetAngles.Y,
|
||||
Math.Max(swaySpeedScaled * fun(percY), minSpeed));
|
||||
angles.Z = Mathf.MoveTowardsAngle(angles.Z, targetAngles.Z,
|
||||
Math.Max(swaySpeedScaled * fun(percZ), minSpeed));
|
||||
} while (remaining > minTime);
|
||||
|
||||
timeRemainder -= remaining;
|
||||
|
||||
Actor.LocalOrientation = Quaternion.Euler(angles);
|
||||
}
|
||||
Actor.LocalOrientation = Quaternion.Euler(angles);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user