Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AlienTed · Mar 26, 2018 at 03:59 PM · coroutinelerpmovetowardsinverse kinematicsmoothdamp

Weapon does't meet the target position when using IK

I've written the following code making the gun move to the guy's shoulder position when he stops shooting and it does work...BUT ONLY ONCE. After that, it starts not to meet the target even though it's coordinates remain the same. I've tried it with Lerp, SmoothDamp, MoveTowards...still don't get where the problem lies. P.S. The gun moves to the shoulder when shooting perfectly, it starts happening when the character stops shooting and tries to go back to the Idle pose.

THE VIDEO of what's going on: https://youtu.be/CheQiomYtm8

THE CODE:

 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);
     }
 }`

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AlienTed · Mar 27, 2018 at 01:08 PM

Problem solved. Thanks to all.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MoveTowards and Lerp not working 1 Answer

Simple Advice needed - Lerp, SmoothDamp, SmoothStep...... etc. 2 Answers

Object rotation with angle constraints 0 Answers

Looking for the optimal smooth incremental movement method. 0 Answers

Why my angle lerp code take only 30% time to finish? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges