Hello! Can u tell me how can i make alternative authoritative movement in one script?
I can’t do it like in video with authoritative input cause my controlling script is in Vector3.
using UnityEngine;
using System.Collections;
using BeardedManStudios.Network;
public class MultiplayerController : NetworkedMonoBehavior {
[Space(5)]
[Header("OffCore")]
public GameObject camera;
public MultiplayerController script;
Animator anim;
[NetSync]
public float x;
[NetSync]
public float y;
[NetSync]
public bool walking;
private GFRectGrid grid;
public float roamingTime = 1.0f; //how long it takes to move from one tile to another
//whether the object is to move or not, to where and how fast
private bool doMove = false;
private Vector3 goal;
private float roamingSpeed;
//cache the transform for performance
private Transform cachedTransform;
void Start(){
if (!IsOwner)
{
camera.SetActive(false);
}
else
{
}
anim = GetComponent<Animator>();
cachedTransform = transform;
grid = ForbiddenTilesExample.movementGrid;
//make a check to prevent getting stuck in a null exception
if(grid){
//snap to the grid no matter where we are
grid.AlignTransform(cachedTransform);
}
}
void Update(){
if(!grid)
return;
if(doMove){
//move towards the desination
Vector3 newPosition = cachedTransform.position;
newPosition.x = Mathf.MoveTowards(cachedTransform.position.x, goal.x, roamingSpeed * Time.deltaTime);
newPosition.y = Mathf.MoveTowards(cachedTransform.position.y, goal.y, roamingSpeed * Time.deltaTime);
cachedTransform.position = newPosition;
//check if we reached the destination (use a certain tolerance so we don't miss the point becase of rounding errors)
if(Mathf.Abs(cachedTransform.position.x - goal.x) < 0.01f && Mathf.Abs(cachedTransform.position.y - goal.y) < 0.01f)
{
doMove = false;
walking = false;
}
//if we did stop moving
} else{
//make sure the time is always positive
if(roamingTime < 0.01f)
roamingTime = 0.01f;
//find the next destination
goal = FindNextFace();
//--- let's check if the goal is allowed, if not we will pick another direction during the next frame ---
if(ForbiddenTilesExample.CheckSquare(goal)){
//calculate speed by dividing distance (one of the two distances will be 0, we need the other one) through time
roamingSpeed = Mathf.Max(Mathf.Abs(cachedTransform.position.x - goal.x), Mathf.Abs(cachedTransform.position.y - goal.y)) / roamingTime;
//resume movement with the new goal
doMove = true;
} else{
Debug.Log("Collision detected.");
walking = false;
}
}
anim.SetBool("isWalking", walking);
anim.SetFloat("y", y);
anim.SetFloat("x", x);
}
Vector3 FindNextFace(){
//we will be operating in grid space, so convert the position
Vector3 newPosition = grid.WorldToGrid(cachedTransform.position);
if(Input.GetKey(KeyCode.LeftArrow)){
y = 0f;
x = -1f;
walking = true;
newPosition = newPosition + new Vector3(-1,0,0);
} else if(Input.GetKey(KeyCode.RightArrow)){
y = 0f;
x = 1f;
walking = true;
newPosition = newPosition + new Vector3(1,0,0);
} else if(Input.GetKey(KeyCode.DownArrow)){
x = 0f;
y = -1f;
walking = true;
newPosition = newPosition + new Vector3(0,-1,0);
} else if(Input.GetKey(KeyCode.UpArrow)){
x = 0f;
y = 1f;
walking = true;
newPosition = newPosition + new Vector3(0,1,0);
}
//if we would wander off beyond the size of the grid turn the other way around
for(int j = 0; j < 2; j++){
if(Mathf.Abs(newPosition[j]) > grid.size[j] / grid.spacing[j])
newPosition[j] -= Mathf.Sign(newPosition[j]) * 2.0f;
}
//return the position in world space
return grid.GridToWorld(newPosition);
}
}
Please help, i need to make this authoritative!! :C