Boss size and rotation not syncing (I think)
In my game i have a boos fight. We have the following script that spawns the boss and handles movement when a player steps into an invisible collider. The boss spawns and rotates and works, but for the player that doesnt spawn the boss the size and rotation are different.
this is the script:
this is the script:
using Normal.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class BossMove : RealtimeComponent<MovingTargetModel>
{
#region Variables
[SerializeField] private float rotationSpeed;
private Vector3 startPosition;
#endregion
void Start()
{
startPosition = transform.position;
}
void Update()
{
MoveTarget();
}
private void MoveTarget()
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
if (model != null)
{
model.rotation = transform.rotation;
}
}
protected override void OnRealtimeModelReplaced(MovingTargetModel previousModel, MovingTargetModel currentModel)
{
if (previousModel != null)
{
previousModel.rotationDidChange -= RotationDidChange;
}
if (currentModel != null)
{
if (currentModel.isFreshModel)
{
currentModel.position = transform.position;
}
UpdateParent();
UpdateRotation(currentModel.rotation);
UpdatePosition(currentModel.position);
UpdateScale(currentModel.scale);
currentModel.rotationDidChange += RotationDidChange;
currentModel.positionDidChange += PositionDidChange;
currentModel.scaleDidChange += ScaleDidChange;
}
}
private void RotationDidChange(MovingTargetModel _model, Quaternion value)
{
UpdateRotation(value);
}
private void PositionDidChange(MovingTargetModel _model, Vector3 value)
{
UpdatePosition(value);
}
private void ScaleDidChange(MovingTargetModel _model, Vector3 value)
{
UpdateScale(value);
}
private void UpdateRotation(Quaternion newRotation)
{
transform.rotation = newRotation;
}
private void UpdatePosition(Vector3 newPosition)
{
transform.position = newPosition;
}
private void UpdateScale(Vector3 newScale)
{
transform.localScale = newScale;
}
private void UpdateParent()
{
string parentString = model.parentNameString;
if (parentString == null) return;
List<string> parents = new List<string>();
foreach (var parent in parentString.Split(" : "))
{
if (parent.Length > 0)
{
parents.Add(parent);
}
}
Transform currParent = null;
foreach (string name in parents)
{
Transform child = FindChildByName(name, currParent);
if (child != null)
{
currParent = child;
}
else
{
Debug.LogWarning("Could not find child: " + name);
return;
}
}
transform.SetParent(currParent);
}
Transform FindChildByName(string name, Transform parent)
{
// If no parent specified, search from the root (scene hierarchy)
if (parent == null)
{
return GameObject.FindGameObjectWithTag("MapOrigin").transform;
}
// Search for the child in the hierarchy
return parent.Find(name);
}
public void SetNewParentString(string newParentString)
{
model.parentNameString = newParentString;
UpdateParent();
}
}using Normal.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class BossMove : RealtimeComponent<MovingTargetModel>
{
#region Variables
[SerializeField] private float rotationSpeed;
private Vector3 startPosition;
#endregion
void Start()
{
startPosition = transform.position;
}
void Update()
{
MoveTarget();
}
private void MoveTarget()
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
if (model != null)
{
model.rotation = transform.rotation;
}
}
protected override void OnRealtimeModelReplaced(MovingTargetModel previousModel, MovingTargetModel currentModel)
{
if (previousModel != null)
{
previousModel.rotationDidChange -= RotationDidChange;
}
if (currentModel != null)
{
if (currentModel.isFreshModel)
{
currentModel.position = transform.position;
}
UpdateParent();
UpdateRotation(currentModel.rotation);
UpdatePosition(currentModel.position);
UpdateScale(currentModel.scale);
currentModel.rotationDidChange += RotationDidChange;
currentModel.positionDidChange += PositionDidChange;
currentModel.scaleDidChange += ScaleDidChange;
}
}
private void RotationDidChange(MovingTargetModel _model, Quaternion value)
{
UpdateRotation(value);
}
private void PositionDidChange(MovingTargetModel _model, Vector3 value)
{
UpdatePosition(value);
}
private void ScaleDidChange(MovingTargetModel _model, Vector3 value)
{
UpdateScale(value);
}
private void UpdateRotation(Quaternion newRotation)
{
transform.rotation = newRotation;
}
private void UpdatePosition(Vector3 newPosition)
{
transform.position = newPosition;
}
private void UpdateScale(Vector3 newScale)
{
transform.localScale = newScale;
}
private void UpdateParent()
{
string parentString = model.parentNameString;
if (parentString == null) return;
List<string> parents = new List<string>();
foreach (var parent in parentString.Split(" : "))
{
if (parent.Length > 0)
{
parents.Add(parent);
}
}
Transform currParent = null;
foreach (string name in parents)
{
Transform child = FindChildByName(name, currParent);
if (child != null)
{
currParent = child;
}
else
{
Debug.LogWarning("Could not find child: " + name);
return;
}
}
transform.SetParent(currParent);
}
Transform FindChildByName(string name, Transform parent)
{
// If no parent specified, search from the root (scene hierarchy)
if (parent == null)
{
return GameObject.FindGameObjectWithTag("MapOrigin").transform;
}
// Search for the child in the hierarchy
return parent.Find(name);
}
public void SetNewParentString(string newParentString)
{
model.parentNameString = newParentString;
UpdateParent();
}
}