Sync IK
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Sync IK
Smiderplaner
2018-02-06 01:32:16
Hello im trying to sync my IK i have attached this script to the player and active it when spawned but it dosent sync rotation so it dosent look up and down, i have tried everything even put a photon view on the look position transform but it dident work either, so can somebody be nice take a look at my script ? please help me with my first online game.
public class IKHandling : Photon.MonoBehaviour
{
Animator anim;
public float lookIKweight;
public float bodyWeight;
public float headWeight;
public float eyesWeight;
public float clampWeight;
public Transform lookPos;
void Start()
{
anim = GetComponent<Animator>();
if (photonView.isMine)
{
lookPos = new GameObject().transform;
lookPos.name = "Look_Position";
lookPos.transform.parent = this.transform;
}
}
void Update()
{
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 15);
lookPos.position = ray.GetPoint(15);
}
void OnAnimatorIK(int layerIndex)
{
anim.SetLookAtWeight(lookIKweight, bodyWeight, headWeight, eyesWeight, clampWeight);
anim.SetLookAtPosition(lookPos.position);
}
}
Smiderplaner
2018-02-06 01:36:33
I have tried for days now to get it work, can somebody give me any hint how to sync position and rotation so everyone can see it? can post more script if necessery
Smiderplaner
2018-02-07 00:32:05
If i have animate physics on and put 1 of the 3 Spines in the model on my photon view on the parent its working but jiddering/teleporting back to orginial position and not syncing full rotation so now im confused becouse its only working with syncing that, shouldent it work with sync the whole model? can somebody point out this problem i can send screenshots if needed.
It's not syncing because from what i see from your script your not telling it what to sync... You instantiate a transform but only to the local player, the remote player is unaware of that object. Once you get the remote to also instantiate the transform then you need to update the remote player the transform's position, however I wouldn't bother with a transform and just have a world position as a Vector3 that way you don't need to instantiate anything and all you need to do is update the remote player the Vector3 look at position
CarterManley
2018-08-03 18:56:28
Ive tried to sync the look at position and was unable to get any response. I also serialized all the ik weights, added photonviews and transform views to the look at transform which is an empty gameobject attached to my camera and the camera rig is attached to my player as a child object. The only gamobject Im photon.instantiating is my player. Heres my script below
CarterManley
2018-08-03 18:57:36
public class AnimatorHook : Photon.PunBehaviour
{
public bool InspectorToolAdjustAimingPos;
public Animator anim;
public StatesManager states;
public Transform looktrans;
float m_h_weight;
float o_h_weight;
float l_weight;
float b_weight;
public Transform rh_target;
public Transform lh_target;
public Transform shoulder;
public Transform aimPivot;
public float angle;
WeaponStats wepStats;
public Vector3 targetDir;
public Quaternion targetRot;
public void Start()
{
states.inp.aimPosition = states.transform.position + transform.forward * 15;
states.inp.aimPosition.y += 1.4f;
}
public void EquipWEP(WeaponStats wep)
{
wepStats = wep;
photonView.RPC("equipWepRPC", PhotonTargets.All);
}
[PunRPC]
public void equipWepRPC()
{
rh_target.localPosition = wepStats.ikPos.pos;
rh_target.localEulerAngles = wepStats.ikPos.rot;
basePosition = wepStats.ikPos.pos;
baseRotation = wepStats.ikPos.rot;
}
CarterManley
2018-08-03 18:58:00
public float count = 5;
public float tickcount;
//doesnt work
void MaybeWorks()
{
if (!photonView.isMine)
{
tickcount += Time.deltaTime;
if (tickcount > count)
{
photonView.RPC("OnAnimatorIKRPC", PhotonTargets.AllBuffered);
// print("yeah");
tickcount = 0;
}
}
}
//doesnt work
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
print("isMineAnimatorView");
stream.SendNext(m_h_weight);
stream.SendNext(o_h_weight);
stream.SendNext(l_weight);
stream.SendNext(b_weight);
stream.SendNext(targetDir);
stream.SendNext(recoilIsInit);
stream.SendNext(targetRot);
if (lh_target)
{
stream.SendNext(lh_target.localRotation);
stream.SendNext(lh_target.localPosition);
}
stream.SendNext(aimPivot.localPosition);
stream.SendNext(rh_target.rotation);
stream.SendNext(rh_target.localPosition);
stream.SendNext(looktrans.localRotation);
stream.SendNext(looktrans.localPosition);
stream.SendNext(offsetPosition);
stream.SendNext(offsetRotation);
stream.SendNext(basePosition);
stream.SendNext(baseRotation);
}
else
{
baseRotation = (Vector3)stream.ReceiveNext();
basePosition = (Vector3)stream.ReceiveNext();
offsetRotation = (Vector3)stream.ReceiveNext();
offsetPosition = (Vector3)stream.ReceiveNext();
recoilIsInit = (bool)stream.ReceiveNext();
targetDir = (Vector3)stream.ReceiveNext();
print(rh_target.localPosition);
m_h_weight = (float)stream.ReceiveNext();
o_h_weight = (float)stream.ReceiveNext();
l_weight = (float)stream.ReceiveNext();
b_weight = (float)stream.ReceiveNext();
if (lh_target)
{
lh_target.localRotation = (Quaternion)stream.ReceiveNext();
lh_target.localPosition = (Vector3)stream.ReceiveNext();
}
rh_target.rotation = (Quaternion)stream.ReceiveNext();
rh_target.localPosition = (Vector3)stream.ReceiveNext();
aimPivot.localPosition = (Vector3)stream.ReceiveNext();
looktrans.localRotation = (Quaternion)stream.ReceiveNext();
looktrans.localPosition = (Vector3)stream.ReceiveNext();
targetRot = (Quaternion)stream.ReceiveNext();
}
}
private void Update()
{
if(photonView.isMine)
states.inp.aimPosition = states.LookTrans.position;
else
states.inp.aimPosition = states.LookTrans.position;
MaybeWorks();
RecoilActual();
if (!states.states.isInteracting)
HandleShoulder();
if (wepStats && InspectorToolAdjustAimingPos)
{
if (states.wSystems.wepSystemReferences.wepHook)
{
if (!states.wSystems.wepSystemReferences.wepHook.FPSS.isFirstPerson)
{
rh_target.localPosition = wepStats.ikPos.pos;
rh_target.localEulerAngles = wepStats.ikPos.rot;
basePosition = wepStats.ikPos.pos;
baseRotation = wepStats.ikPos.rot;
}
if (states.wSystems.wepSystemReferences.isFirstPerson)
{
FpsIkPos();
}
}
}
}
CarterManley
2018-08-03 18:58:35
void FpsIkPos()
{
if (!states.states.isAiming && states.states.isAiming1)
{
rh_target.localPosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSRegularIk.pos;
rh_target.localEulerAngles = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSRegularIk.rot;
basePosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSRegularIk.pos;
baseRotation = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSRegularIk.rot;
}
if (states.states.isAiming1 && !states.states.isAiming)
{
rh_target.localPosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSNoAimFiringIk.pos;
rh_target.localEulerAngles = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSNoAimFiringIk.rot;
basePosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSNoAimFiringIk.pos;
baseRotation = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSNoAimFiringIk.rot;
}
if (states.states.isAiming)
{
rh_target.localPosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSAIMIK.pos;
rh_target.localEulerAngles = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSAIMIK.rot;
basePosition = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSAIMIK.pos;
baseRotation = states.wSystems.wepSystemReferences.wepHook.FPSS.FPSAIMIK.rot;
}
}
void HandleShoulder()
{
HandleShoulderPosition();
HandleShoulderRotation();
}
void HandleShoulderPosition()
{
if(photonView.isMine)
aimPivot.position = shoulder.position;
else
aimPivot.position = shoulder.position;
}
void HandleShoulderRotation()
{
if (photonView.isMine)
{
targetDir = looktrans.position - aimPivot.position;
if (targetDir == Vector3.zero)
targetDir = aimPivot.forward;
targetRot = Quaternion.LookRotation(targetDir);
aimPivot.rotation = Quaternion.Slerp(aimPivot.rotation, targetRot, states.delta * 15);
}
else
{
targetDir = looktrans.position - aimPivot.position;
if (targetDir == Vector3.zero)
targetDir = aimPivot.forward;
targetRot = Quaternion.LookRotation(targetDir);
aimPivot.rotation = Quaternion.Slerp(aimPivot.rotation, targetRot, states.delta * 15);
}
}
void HandleWeights()
{
float t_l_weight = 0;
float t_m_weight = 0;
if (lh_target != null)
o_h_weight = 1;
else
o_h_weight = 0;
if (states.states.isInteracting)
{
m_h_weight = 0;
o_h_weight = 0;
l_weight = 0;
}
else
{
t_m_weight = 1;
b_weight = 0.4f;
}
Vector3 ld = states.inp.aimPosition - states.transform.position;
angle = Vector3.Angle(states.transform.forward, ld);
if (angle < 85)
{
t_l_weight = 1;
states.inAngle = true;
}
if (angle > 85)
{
t_m_weight = 0;
states.inAngle = false;
}
l_weight = Mathf.Lerp(l_weight, t_l_weight, states.delta * 1.5f);
m_h_weight = Mathf.Lerp(m_h_weight, t_m_weight, states.delta * 5);
}
private void OnAnimatorIK()
{
if (GetComponent<PhotonView>().isMine)
{
anim.SetLookAtWeight(l_weight, b_weight, 1, 1, 1);
anim.SetLookAtPosition(looktrans.position);
HandleWeights();
if (!states.states.isInteracting && states.states.isRifle && states.states.isAiming && !states.states.isJumping && states.grounded || states.wSystems.defenseMode)
{
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
UpdateIK(AvatarIKGoal.RightHand, rh_target, m_h_weight);
}
if (!states.states.isPistol && !states.states.isSword && !states.states.isInteracting && !states.states.isJumping && states.grounded)
{
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
}
if (states.states.isPistol || states.states.isSword)
{
if (!states.states.isInteracting)
{
print("HEREPISTOL");
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
if (states.states.isAiming || states.states.isAiming1)
{
UpdateIK(AvatarIKGoal.RightHand, rh_target, m_h_weight);
}
}
}
}
else
{
anim.SetLookAtWeight(l_weight, b_weight, 1, 1, 1);
anim.SetLookAtPosition(looktrans.position);
HandleWeights();
if (!states.states.isInteracting && states.states.isRifle && states.states.isAiming && !states.states.isJumping && states.grounded || states.wSystems.defenseMode)
{
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
UpdateIK(AvatarIKGoal.RightHand, rh_target, m_h_weight);
}
if (!states.states.isPistol && !states.states.isSword && !states.states.isInteracting && !states.states.isJumping && states.grounded)
{
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
}
if (states.states.isPistol || states.states.isSword)
{
if (!states.states.isInteracting)
{
print("HEREPISTOL");
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
if (states.states.isAiming || states.states.isAiming1)
{
UpdateIK(AvatarIKGoal.RightHand, rh_target, m_h_weight);
}
}
}
}
}
CarterManley
2018-08-03 18:58:57
[PunRPC]
void OnAnimatorIKRPC()
{
if (photonView.isMine == false)
{
aimPivot.position = shoulder.position;
anim.SetLookAtWeight(l_weight, b_weight, 1, 1, 1);
anim.SetLookAtPosition(looktrans.position);
HandleWeights();
HandleShoulder();
if (lh_target != null)
{
UpdateIK(AvatarIKGoal.LeftHand, lh_target, o_h_weight);
}
UpdateIK(AvatarIKGoal.RightHand, rh_target, m_h_weight);
}
}
void UpdateIK(AvatarIKGoal goal, Transform t, float w)
{
if (states.states.isPistol || states.states.isRifle || states.states.onMotorBike || states.Vaultingforcetime)
{
anim.SetIKPositionWeight(goal, w);
anim.SetIKRotationWeight(goal, w);
anim.SetIKPosition(goal, t.position);
anim.SetIKRotation(goal, t.rotation);
}
}
public void Tick()
{
}
#region Recoil
float recoilT;
public Vector3 offsetPosition;
public Vector3 offsetRotation;
public Vector3 basePosition;
public Vector3 baseRotation;
public bool recoilIsInit;
public void RecoilAnim()
{
if (!recoilIsInit)
{
recoilIsInit = true;
recoilT = 0;
offsetPosition = Vector3.zero;
}
}
public void RecoilActual()
{
if (recoilIsInit)
{
recoilT += states.delta * 3;
if (recoilT > 1)
{
recoilT = 1;
recoilIsInit = false;
}
offsetPosition = Vector3.forward * wepStats.recoilZ.Evaluate(recoilT);
offsetRotation = Vector3.right * 90 * -wepStats.recoilY.Evaluate(recoilT);
rh_target.localPosition = basePosition + offsetPosition;
rh_target.localEulerAngles = baseRotation + offsetRotation;
}
}
#endregion
}
}
CarterManley
2018-08-03 21:43:48
Can i give someone money to help?? this is insane!
CarterManley
2018-08-05 05:36:46
Values from onAnimatorIk to Update ik werent being passed through the network. I was able to solve it finally XD Thanks for the help!
can you post your fixed code?
Back to top