added: units can now have their movement settings set through rpc

This commit is contained in:
Chris
2025-10-08 22:55:55 -04:00
parent eb7ff08b50
commit 44e3a3ef7a
4 changed files with 119 additions and 38 deletions

View File

@@ -1,13 +1,25 @@
using Unity.Netcode; using Unity.Netcode;
namespace Reset.Units{
public class UnitComponent : NetworkBehaviour{ public class UnitComponent : NetworkBehaviour{
private bool enabledAsHost = true; private bool enabledAsHost = true;
void DisableComponents(){ private Unit _unit;
internal Unit Unit{
get {
if (_unit != null) {
return _unit;
}
_unit = GetComponent<Unit>();
return _unit;
}
}
void DisableComponent(){
enabledAsHost = false; enabledAsHost = false;
} }
void Update(){
} }
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using Reset.Core; using Reset.Core;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Netcode;
using UnityEngine; using UnityEngine;
namespace Reset.Units{ namespace Reset.Units{
@@ -10,6 +11,9 @@ namespace Reset.Units{
public bool lockonDebug{ get; set; } = true; public bool lockonDebug{ get; set; } = true;
public float lockonRaycastVerticalOffset{ get; set; } = 1f; public float lockonRaycastVerticalOffset{ get; set; } = 1f;
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
public Animator testModelAnimator; public Animator testModelAnimator;
public override void UnitStart(){ public override void UnitStart(){
@@ -29,11 +33,15 @@ namespace Reset.Units{
public void TakeDamage(DamageSource source){ public void TakeDamage(DamageSource source){
try { try {
((IKillable)this).currentHealth -= source.damageDealt; currentHealth -= source.damageDealt;
if (UnitIsNetworked()){
SetHealthRpc(currentHealth);
}
testModelAnimator.SetTrigger("Hit"); testModelAnimator.SetTrigger("Hit");
if (((IKillable)this).currentHealth <= 0) { if (currentHealth <= 0) {
Kill(); Kill();
} }
} catch (Exception e) { } catch (Exception e) {
@@ -41,11 +49,14 @@ namespace Reset.Units{
} }
} }
[Rpc(SendTo.Everyone)]
void SetHealthRpc(float newHealth){
currentHealth = newHealth;
}
public void Kill(){ public void Kill(){
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
} }
} }

View File

@@ -28,9 +28,5 @@ namespace Reset.Units{
Color.blue); Color.blue);
} }
} }
private void InternalUpdate(){
Debug.Log("is this possible");
}
} }
} }

View File

@@ -2,9 +2,10 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Reset.Core.Tools; using Reset.Core.Tools;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Netcode;
namespace Reset.Units{ namespace Reset.Units{
public class UnitMovementHandler : MonoBehaviour{ public class UnitMovementHandler : UnitComponent{
[ShowInInspector, InlineProperty, HideLabel, FoldoutGroup("Resolved Movement", expanded: true)] [ShowInInspector, InlineProperty, HideLabel, FoldoutGroup("Resolved Movement", expanded: true)]
public ResolvedMovement resolvedMovement; public ResolvedMovement resolvedMovement;
@@ -207,11 +208,24 @@ namespace Reset.Units{
// Setting absolute to true will cause the current gravity to snap to the new gravity value. // Setting absolute to true will cause the current gravity to snap to the new gravity value.
// Keeping it false will make it apply additively to the current gravity. Both options use relativty for linear interpolation. // Keeping it false will make it apply additively to the current gravity. Both options use relativty for linear interpolation.
public void SetNewGravity(float value, float relativity, bool absolute){ // new public void SetNewGravity(float value, float relativity, bool absolute){ // new
float newGravity;
if (absolute){ if (absolute){
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity); newGravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity);
} else { } else {
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity); newGravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity);
} }
if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) {
SetNewGravityRpc(newGravity);
} else {
resolvedMovement.gravity = newGravity;
}
}
[Rpc(SendTo.Owner)]
public void SetNewGravityRpc(float value){
resolvedMovement.gravity = value;
} }
public void SetNewDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default, bool relativeToRotation = true){ // new public void SetNewDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default, bool relativeToRotation = true){ // new
@@ -223,31 +237,67 @@ namespace Reset.Units{
relativeValue = relativeTo + value; relativeValue = relativeTo + value;
} }
Vector2 newValue;
if (absolute){ if (absolute){
resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, relativeValue, relativity); newValue = Vector2.Lerp(resolvedMovement.moveDirection.World, relativeValue, relativity);
} else { } else {
resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, resolvedMovement.moveDirection.World + relativeValue, relativity); newValue = Vector2.Lerp(resolvedMovement.moveDirection.World, resolvedMovement.moveDirection.World + relativeValue, relativity);
} }
Debug.Log(resolvedMovement.moveDirection.World); if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) {
SetNewDirectionRpc(newValue);
} else {
resolvedMovement.moveDirection.World = newValue;
}
}
[Rpc(SendTo.Owner)]
public void SetNewDirectionRpc(Vector2 value){
resolvedMovement.moveDirection.World = value;
} }
public void SetNewRawDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default){ // new public void SetNewRawDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default){ // new
Vector2 relativeValue = relativeTo + value; Vector2 relativeValue = relativeTo + value;
Vector2 newValue;
if (absolute){ if (absolute){
resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, relativeValue, relativity); newValue = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, relativeValue, relativity);
} else { } else {
resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, resolvedMovement.moveDirection.RawWorld + relativeValue, relativity); newValue = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, resolvedMovement.moveDirection.RawWorld + relativeValue, relativity);
} }
if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) {
SetNewRawDirectionRpc(newValue);
} else {
resolvedMovement.moveDirection.RawWorld = newValue;
}
}
[Rpc(SendTo.Owner)]
public void SetNewRawDirectionRpc(Vector2 value){
resolvedMovement.moveDirection.RawWorld = value;
} }
public void SetNewSpeed(float value, float relativity, bool absolute, float relativeTo = Mathf.Infinity){ // new public void SetNewSpeed(float value, float relativity, bool absolute, float relativeTo = Mathf.Infinity){ // new
float newSpeed;
if (absolute){ if (absolute){
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, value, relativity); newSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, value, relativity);
} else { } else {
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, resolvedMovement.moveSpeed + value, relativity); newSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, resolvedMovement.moveSpeed + value, relativity);
} }
if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) {
SetNewSpeedRpc(newSpeed);
} else {
resolvedMovement.moveSpeed = newSpeed;
}
}
[Rpc(SendTo.Owner)]
public void SetNewSpeedRpc(float value){
resolvedMovement.moveSpeed = value;
} }
public void SetNewRotation(Vector3 value, float relativity, bool absolute, Vector3 relativeTo = default){ // new public void SetNewRotation(Vector3 value, float relativity, bool absolute, Vector3 relativeTo = default){ // new
@@ -257,12 +307,24 @@ namespace Reset.Units{
valueAsQuaternion = Quaternion.LookRotation(relativeTo) * valueAsQuaternion; valueAsQuaternion = Quaternion.LookRotation(relativeTo) * valueAsQuaternion;
} }
Quaternion newRotation;
if (absolute){ if (absolute){
resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, valueAsQuaternion, relativity); newRotation = Quaternion.Lerp(resolvedMovement.rotation, valueAsQuaternion, relativity);
} else { } else {
resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, resolvedMovement.rotation * valueAsQuaternion, relativity); newRotation = Quaternion.Lerp(resolvedMovement.rotation, resolvedMovement.rotation * valueAsQuaternion, relativity);
} }
if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) {
SetNewRotationRpc(newRotation);
} else {
resolvedMovement.rotation = newRotation;
}
}
[Rpc(SendTo.Owner)]
public void SetNewRotationRpc(Quaternion value){
resolvedMovement.rotation = value;
} }
public void SetSpecifiedRotation(Vector3 inputRotation){ public void SetSpecifiedRotation(Vector3 inputRotation){