首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IK时,武器不满足目标位置

使用IK时,武器不满足目标位置
EN

Stack Overflow用户
提问于 2018-03-26 15:25:52
回答 1查看 361关注 0票数 1

我已经写了下面的代码,让枪在他停止射击时移到他的肩膀位置,而且它只做了一次work...BUT。在此之后,即使坐标保持不变,它也不会达到目标。我试过LerpSmoothDampMoveTowards...still不知道问题出在哪里。当射击完美的时候,枪会移到肩膀上,当角色停止射击并试图回到懒散的姿势时,它就开始发生了。

编辑:原来rotation...or也有问题,可能只是旋转。我现在都不知道。

正在发生的事情的视频:https://youtu.be/CheQiomYtm8

守则:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnitControl;

public class BlastIKController : MonoBehaviour
{
    public WeaponState wpState;
    public GameObject weapon;
    public GameObject RightShoulder;
    public GameObject HumanSpine;
    public GameObject WeaponSpawn;
    public LayerMask lmask;
    public BlastIKHandler ikHandle;
    public Material targetMat;
    public Material defMat;
    public GameObject target;

    public GameObject WeaponIdle;
    public bool isShooting = false;
    //public bool InIdle = true;

    LineRenderer ShootLine;

    public GameObject WeaponInstance;

    Animator anim;

    public float speedMove;
    public float speedRot;

    // Use this for initialization
    void Awake()
    {
        GameObject weaponInst = Instantiate(weapon, WeaponSpawn.transform);
        WeaponInstance = weaponInst;
        WeaponInstance.transform.localPosition = new Vector3(0, 0, 0);
        wpState = weaponInst.GetComponent<WeaponState>();
        ikHandle = this.GetComponent<BlastIKHandler>();
        ShootLine = this.GetComponent<LineRenderer>();
        anim = this.GetComponent<Animator>();
        ikHandle.RightShoulder = RightShoulder;
        ikHandle.leftHandTarget = wpState.leftHandIdle.transform;
        ikHandle.rightHandTarget = wpState.rightHandTarget.transform;

        //Позиция оружия
        wpState.shoulder.transform.position = ikHandle.WeaponIdlePos.position;
        wpState.shoulder.transform.rotation = ikHandle.WeaponIdlePos.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        #region SearchTarget
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, lmask))
        {
            if (hit.collider.gameObject.tag == "Target")
            {
                ShootLine.positionCount = 2;
                ShootLine.SetPosition(0, HumanSpine.transform.position);
                ShootLine.SetPosition(1, hit.collider.gameObject.transform.position);

                if (Input.GetMouseButton(0))
                {
                    if (target == null)
                    {
                        target = hit.collider.gameObject;
                        MeshRenderer ms = hit.collider.gameObject.GetComponent<MeshRenderer>();
                        ms.material = targetMat;
                        ikHandle.targetPos = hit.collider.gameObject;
                    }
                    else
                    {
                        MeshRenderer ms = target.GetComponent<MeshRenderer>();
                        ms.material = defMat;
                        target = hit.collider.gameObject;
                        ms = target.GetComponent<MeshRenderer>();
                        ms.material = targetMat;
                        ikHandle.targetPos = hit.collider.gameObject;
                    }
                }
            }
        }
        #endregion

        #region Shooting
        Shooting();

        if (isShooting)
        {
            if (target != null)
            {
                bool isShoot = anim.GetBool("Shoot");
                if (!isShoot)
                {
                    StartCoroutine(MoveToShoot(RightShoulder.transform.position));
                    ikHandle.leftHandTarget = wpState.leftHandTarget.transform;
                    anim.SetBool("Shoot", true);
                    //InIdle = false;
                }
            }
        }
        else
        {
           // float stepMove = speedMove * Time.deltaTime;
           // wpState.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
           // //if (!InIdle)
           // //{
           //     //    StartCoroutine(MoveToIdle(ikHandle.WeaponIdlePos.position));

           //// }
           // //InIdle = true;
           // //float stepMove = speedMove * Time.deltaTime;
           // //while (wpState.shoulder.transform.position != ikHandle.WeaponIdlePos.position)
           // //{
           // //    wpState.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
           // //}
           // ////wpState.shoulder.transform.position = ikHandle.WeaponIdlePos.position;
           // ////wpState.shoulder.transform.position = Vector3.MoveTowards(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
           // float stepRot = speedRot * Time.deltaTime;
           // //while (wpState.shoulder.transform.rotation != ikHandle.WeaponIdlePos.rotation)
           // //{
           //     wpState.shoulder.transform.rotation = Quaternion.Slerp(wpState.shoulder.transform.rotation, ikHandle.WeaponIdlePos.rotation, stepRot);
           // //}
           // ////wpState.shoulder.transform.rotation = ikHandle.WeaponIdlePos.rotation;
           // ikHandle.leftHandTarget = wpState.leftHandIdle.transform;
           // anim.SetBool("Shoot", false);          
        }
        #endregion
    }

    void LateUpdate()
    {
        if (!isShooting)
        {
            float stepMove = speedMove * Time.deltaTime;
            stepMove += Time.deltaTime / speedMove;
            Vector3 velocity = Vector3.zero;
            //.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
            wpState.shoulder.transform.position = Vector3.MoveTowards(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
            //wpState.shoulder.transform.position = Vector3.SmoothDamp(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, ref velocity, stepMove);
           // wpState.shoulder.transform.position = Vector3.SmoothDamp()

            float stepRot = speedRot * Time.deltaTime;
            wpState.shoulder.transform.rotation = Quaternion.Slerp(wpState.shoulder.transform.rotation, ikHandle.WeaponIdlePos.rotation, stepRot);

            ikHandle.leftHandTarget = wpState.leftHandIdle.transform;
            anim.SetBool("Shoot", false);
        }
    }

    void Shooting()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            isShooting = !isShooting;
        }
    }

    IEnumerator MoveToShoot(Vector3 WPposition)
    {
        float step = speedMove * Time.deltaTime;
        while (wpState.shoulder.transform.position != WPposition)
        {
            wpState.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, WPposition, step);
            Vector3 relativeWeaponPos = ikHandle.targetPos.transform.position - wpState.shoulder.transform.position;
            Quaternion WeaponRotation = Quaternion.LookRotation(relativeWeaponPos);
            wpState.shoulder.transform.rotation = Quaternion.Slerp(wpState.shoulder.transform.rotation, WeaponRotation, step);
            yield return null;
        }
    }

    IEnumerator MoveToIdle(Vector3 WPposition)
    {
        float stepMove = speedMove * Time.deltaTime;
        float stepRot = speedRot * Time.deltaTime;
        while (wpState.shoulder.transform.position != WPposition)
        {
            wpState.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, WPposition, stepMove);
            wpState.shoulder.transform.rotation = Quaternion.Slerp(wpState.shoulder.transform.rotation, ikHandle.WeaponIdlePos.transform.rotation, stepRot);
            yield return null;
        }           
        wpState.shoulder.transform.position = Vector3.Lerp(wpState.shoulder.transform.position, ikHandle.WeaponIdlePos.position, stepMove);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-27 13:13:25

所以,它是如下。事实证明,这些协同机制在更新中被混淆了,并且它们同时工作。

所以.我从代码中删除了所有的协同线,然后在更新中使用Lerp和sLerp进行移动和旋转。

但有一件事,我还必须添加一个检查,以满足这两个职位,之后,它开始拍摄,只有那时。

如果你愿意的话,我可以添加我后来修改的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49495118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档