Вопрос в том, как сделать так, чтобы персонаж нацеливал винтовку точно в цель? Т.е из пустышки в дуле винтовки выпускается луч(не на цель, а просто вперед), и нужно чтобы луч пересекал цель. По сути, персонаж и так следит за целью, но надо, чтобы именно луч из винтовки пересекал цель. По задумке, работать прицеливание должно в обе стороны, т.е и когда цель перемещается вверх-вниз, и влево-вправо.
Винтовка прикреплена к кости правой руки персонажа.
Как я понял, левая рука не играет роли(она просто следует к своей пустышке), а прицеливаться надо именно правой рукой, только вот как? С другой стороны, если посмотреть на реальный пример стрельбы с винтовки, то руки находятся в одном неподвижном положении, а двигается корпус. Но как сделать "прицеливание корпусом" для меня еще большая загадка))
Я уже много что перепробовал, но ничего не помогло...
Картинка, для лучшего понимания, что там вообще происходит
https://ibb.co/qRJpdqN
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitIK : MonoBehaviour
{
private Animator anim;
private bool isAiming;
public UnitInventory inventory;
public Transform leftHand;
public Transform leftHandTarget;
public Quaternion leftHandRotation;
public Transform rightHand;
public float rightHandWeight = 1;
public float leftHandWeight = 1;
public Transform shoulder;
public Transform aimPivot;
public Transform target;
void Start()
{
anim = GetComponent<Animator>();
isAiming = anim.GetBool("isAiming");
shoulder = anim.GetBoneTransform(HumanBodyBones.RightShoulder).transform;
aimPivot = new GameObject().transform;
aimPivot.name = "aim pivot";
aimPivot.transform.parent = transform;
aimPivot.transform.localPosition = new Vector3(0, 0, 0);
rightHand = new GameObject().transform;
rightHand.name = "rightHand";
rightHand.transform.parent = aimPivot;
rightHand.localPosition = new Vector3(0, 0, 0);
leftHand = new GameObject().transform;
leftHand.name = "leftHand";
leftHand.transform.parent = aimPivot;
rightHand.localPosition = inventory.firstWeapon.rightHandPos;
Quaternion rightHandRot = Quaternion.Euler(inventory.firstWeapon.rightHandRotation.x, inventory.firstWeapon.rightHandRotation.y, inventory.firstWeapon.rightHandRotation.z);
rightHand.localRotation = rightHandRot;
}
void Update()
{
leftHandRotation = leftHandTarget.rotation;
leftHand.position = leftHandTarget.position;
if (Input.GetMouseButtonDown(1))
{
isAiming = !isAiming;
anim.SetBool("isAiming", isAiming);
}
if (isAiming)
rightHandWeight += Time.deltaTime * 2;
else rightHandWeight -= Time.deltaTime * 2;
rightHandWeight = Mathf.Clamp(rightHandWeight, 0, 1);
}
void OnAnimatorIK()
{
aimPivot.position = shoulder.position;
if (isAiming)
{
aimPivot.LookAt(target);
anim.SetLookAtWeight(1f, 1f, 1f);
anim.SetLookAtPosition(target.position);
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKPosition(AvatarIKGoal.RightHand, rightHand.position);
anim.SetIKRotation(AvatarIKGoal.RightHand, rightHand.rotation);
anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHand.position);
anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandRotation);
}
else
{
anim.SetLookAtWeight(.3f, .3f, .3f);
anim.SetLookAtPosition(target.position);
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKPosition(AvatarIKGoal.RightHand, rightHand.position);
anim.SetIKRotation(AvatarIKGoal.RightHand, rightHand.rotation);
anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHand.position);
anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandRotation);
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class UnitIK : MonoBehaviour
{
private Animator anim;
private bool isAiming;
public UnitInventory inventory;
public Transform leftHand;
public Transform leftHandTarget;
public Quaternion leftHandRotation;
public Transform rightHand;
public float rightHandWeight = 1;
public float leftHandWeight = 1;
public Transform shoulder;
public Transform aimPivot;
public Transform target;
void Start()
{
anim = GetComponent<Animator>();
isAiming = anim.GetBool("isAiming");
shoulder = anim.GetBoneTransform(HumanBodyBones.RightShoulder).transform;
aimPivot = new GameObject().transform;
aimPivot.name = "aim pivot";
aimPivot.transform.parent = transform;
aimPivot.transform.localPosition = new Vector3(0, 0, 0);
rightHand = new GameObject().transform;
rightHand.name = "rightHand";
rightHand.transform.parent = aimPivot;
rightHand.localPosition = new Vector3(0, 0, 0);
leftHand = new GameObject().transform;
leftHand.name = "leftHand";
leftHand.transform.parent = aimPivot;
rightHand.localPosition = inventory.firstWeapon.rightHandPos;
Quaternion rightHandRot = Quaternion.Euler(inventory.firstWeapon.rightHandRotation.x, inventory.firstWeapon.rightHandRotation.y, inventory.firstWeapon.rightHandRotation.z);
rightHand.localRotation = rightHandRot;
}
void Update()
{
leftHandRotation = leftHandTarget.rotation;
leftHand.position = leftHandTarget.position;
if (Input.GetMouseButtonDown(1))
{
isAiming = !isAiming;
anim.SetBool("isAiming", isAiming);
}
if (isAiming)
rightHandWeight += Time.deltaTime * 2;
else rightHandWeight -= Time.deltaTime * 2;
rightHandWeight = Mathf.Clamp(rightHandWeight, 0, 1);
}
void OnAnimatorIK()
{
aimPivot.position = shoulder.position;
if (isAiming)
{
aimPivot.LookAt(target);
anim.SetLookAtWeight(1f, 1f, 1f);
anim.SetLookAtPosition(target.position);
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKPosition(AvatarIKGoal.RightHand, rightHand.position);
anim.SetIKRotation(AvatarIKGoal.RightHand, rightHand.rotation);
anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHand.position);
anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandRotation);
}
else
{
anim.SetLookAtWeight(.3f, .3f, .3f);
anim.SetLookAtPosition(target.position);
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKPosition(AvatarIKGoal.RightHand, rightHand.position);
anim.SetIKRotation(AvatarIKGoal.RightHand, rightHand.rotation);
anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHand.position);
anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandRotation);
}
}
}
Возможно, нужно скинуть что-то еще для лучшего прояснения ситуации? Заранее спасибо за любую помощь!