- Home /
Question by
Eco-Editor · Aug 15, 2017 at 07:19 AM ·
prefabmultiplayervrspawnavatar
Where did the avatars go? Multiplayer spawn prefabs
Hi all,
Why this is happening? And how can this be fixed?
This is the script that makes the avatar spawn outside the game area.
Can someone please see what in this script causing the problem?
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.AI;
public class Avatar_SyncMotion : NetworkBehaviour
{
[SyncVar] private Vector3 syncPos;//the position to sync
[SyncVar] private float syncYRot; // the rottation to sync
private Vector3 lastPos; // last position of the avatare
private Quaternion lastRot; // last rotation of the avatae
private Transform myTransform; //the ternsform of the avatar
private float lerpRate = 10;
private float posThreshold = 0.5f; //this is == 0.5, I'm making it 0.01
private float rotThreshold = 5;
private NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
agent.Warp(transform.position);
}
// Use this for initialization
void Start()
{
myTransform = transform;
}
// Update is called once per frame
void Update()
{
TransmitMotion();
LerpMotion();
}
void TransmitMotion()
{
if (!isServer)
{
return;
}
if (Vector3.Distance(myTransform.position, lastPos) > posThreshold || Quaternion.Angle(myTransform.rotation, lastRot) > rotThreshold)
{
lastPos = myTransform.position;
lastRot = myTransform.rotation;
syncPos = myTransform.position;
syncYRot = myTransform.localEulerAngles.y;
}
}
void LerpMotion()
{
if (isServer)
{
return;
}
myTransform.position = Vector3.Lerp(myTransform.position, syncPos, Time.deltaTime * lerpRate);
Vector3 newRot = new Vector3(0, syncYRot, 0);
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.Euler(newRot), Time.deltaTime * lerpRate);
}
}
Thanks!
Comment
Your answer
Follow this Question
Related Questions
Prefab spawning without running script (uses VR controllers) 1 Answer
Why scene objects in multiplayer will not spawn, if enabled after scene is loaded? 2 Answers
Custom avatar on HTC Vive CameraRig 3 Answers
Multiplayer prefab spawns with wrong orientation 1 Answer
Multiplayer avatar's transform settings 0 Answers