浏览代码

fixing targeting (rigidbodies now come along with gameobjects as they move), and adding smoothing to graphics representation of entities

/main
David Woodruff 3 年前
当前提交
d1317bb5
共有 4 个文件被更改,包括 75 次插入17 次删除
  1. 45
      Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs
  2. 30
      Assets/BossRoom/Scripts/Client/ClientInputSender.cs
  3. 7
      Assets/BossRoom/Scripts/Client/Game/Character/ClientCharacterMovement.cs
  4. 10
      Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs

45
Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs


private NetworkCharacterState m_NetState;
private Animator m_ClientVisualsAnimator;
private CinemachineVirtualCamera m_MainCamera;
private Transform m_Parent;
private const float MAX_VIZ_SPEED = 4; //max speed at which we will chase the parent transform.
private const float MAX_ROT_SPEED = 280; //max angular speed at which we will rotate, in degrees/second.
/// <inheritdoc />
public override void NetworkStart()

m_NetState = this.transform.parent.gameObject.GetComponent<NetworkCharacterState>();
m_NetState.DoActionEventClient += this.PerformActionFX;
GetComponent<ModelSwap>();
//GetComponents<ModelSwap>
//we want to follow our parent on a spring, which means it can't be directly in the transform hierarchy.
m_Parent = transform.parent;
transform.parent = null;
if (IsLocalPlayer)
{
AttachCamera();

void Update()
{
// TODO Needs core sdk support. This and rotation should grab the interpolated value of network position based on the last received snapshots.
transform.position = m_NetState.NetworkPosition.Value;
SmoothMove();
transform.rotation = Quaternion.Euler(0, m_NetState.NetworkRotationY.Value, 0);
if (m_ClientVisualsAnimator)
{

ZoomCamera(scroll);
}
}
private void SmoothMove()
{
if (m_Parent == null) { return; } //parent was destroyed, and we won't be far behind.
var pos_diff = m_Parent.transform.position - transform.position;
var angle_diff = Quaternion.Angle(m_Parent.transform.rotation, transform.rotation);
float time_delta = Time.deltaTime;
float pos_diff_mag = pos_diff.magnitude;
if( pos_diff_mag > 0 )
{
float max_move = time_delta * MAX_VIZ_SPEED;
float move_dist = Mathf.Min(max_move, pos_diff_mag);
pos_diff *= (move_dist / pos_diff_mag);
transform.position += pos_diff;
}
if( angle_diff > 0 )
{
float max_angle_move = time_delta * MAX_ROT_SPEED;
float angle_move = Mathf.Min(max_angle_move, angle_diff);
float t = angle_move / angle_diff;
transform.rotation = Quaternion.Slerp(transform.rotation, m_Parent.transform.rotation, t);
}
}
private void AttachCamera()

30
Assets/BossRoom/Scripts/Client/ClientInputSender.cs


{
private NetworkCharacterState m_NetworkCharacter;
/// <summary>
/// We detect clicks in Update (because you can miss single discrete clicks in FixedUpdate). But we need to
/// raycast in FixedUpdate, because raycasts done in Update won't work reliably.
/// This nullable vector will be set to a screen coordinate when an attack click was made.
/// </summary>
private System.Nullable<Vector3> m_AttackClickRequest;
public override void NetworkStart()
{
// TODO Don't use NetworkedBehaviour for just NetworkStart [GOMPS-81]

"MLAPI_INTERNAL");
}
}
}
private void Update()
{
if (Input.GetMouseButtonDown(1))
if (m_AttackClickRequest != null)
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && GetTargetObject(ref hit) != 0)
if (Physics.Raycast(Camera.main.ScreenPointToRay(m_AttackClickRequest.Value), out hit) && GetTargetObject(ref hit) != 0)
{
//these two actions will queue one after the other, causing us to run over to our target and take a swing.
var chase_data = new ActionRequestData();

m_NetworkCharacter.C2S_DoAction(ref data);
}
m_AttackClickRequest = null;
}
}
private void Update()
{
//we do this in "Update" rather than "FixedUpdate" because discrete clicks can be missed in FixedUpdate.
if (Input.GetMouseButtonDown(1))
{
m_AttackClickRequest = Input.mousePosition;
}
}

private ulong GetTargetObject(ref RaycastHit hit )
{
if( hit.collider == null ) { return 0; }
if( hit.collider == null ) { return 0; }
if( targetObj == null ) { return 0; }
if( targetObj == null ) { return 0; }
return targetObj.NetworkId;
}
}

7
Assets/BossRoom/Scripts/Client/Game/Character/ClientCharacterMovement.cs


/// <summary>
/// Client-side of character movement game logic.
/// </summary>
[RequireComponent(typeof(NetworkCharacterState))]
[RequireComponent(typeof(NetworkCharacterState), typeof(Rigidbody))]
private Rigidbody m_Rigidbody;
// Start is called before the first frame update

m_Rigidbody = GetComponent<Rigidbody>();
}
public override void NetworkStart()

{
transform.position = m_NetState.NetworkPosition.Value;
transform.rotation = Quaternion.Euler(0, m_NetState.NetworkRotationY.Value, 0);
m_Rigidbody.position = transform.position;
m_Rigidbody.rotation = transform.rotation;
}
}
}

10
Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs


/// <summary>
/// Component responsible for moving a character on the server side based on inputs.
/// </summary>
[RequireComponent(typeof(NetworkCharacterState), typeof(NavMeshAgent), typeof(ServerCharacter))]
[RequireComponent(typeof(NetworkCharacterState), typeof(NavMeshAgent), typeof(ServerCharacter)), RequireComponent(typeof(Rigidbody))]
private Rigidbody m_Rigidbody;
private NetworkCharacterState m_NetworkCharacterState;
private NavMeshPath m_DesiredMovementPath;

m_NavMeshAgent = GetComponent<NavMeshAgent>();
m_NetworkCharacterState = GetComponent<NetworkCharacterState>();
m_CharLogic = GetComponent<ServerCharacter>();
m_Rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()

m_NavMeshAgent.Move(movementVector);
transform.rotation = Quaternion.LookRotation(movementVector);
//fixme--is this right? If I don't do this the Rigidbody is "left behind", and doesn't move with the GameObject.
//also see ClientCharacterMovement before deleting this comment.
m_Rigidbody.position = transform.position;
m_Rigidbody.rotation = transform.rotation;
m_NavMeshAgent.CalculatePath(corners[corners.Length - 1], m_DesiredMovementPath);
}
}
正在加载...
取消
保存