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 /
avatar image
0
Question by Broseph1010 · Jul 08, 2018 at 04:02 AM · 3dpositioning

How can I move this part in a rectangle when I press a key? [C#]

I am trying to get an object to move in a rectangle when I use either the"Horizontal" or "Vertical" input axes. What I have tried keeps on adding to the position, making the localPosition extremely high, eventually to an error. If I am doing something wrong, please tell me. Any help will be appreciated. (The part is an inverse kinematic tracker part)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class IkProceduralAnimation : MonoBehaviour {
     Animator anim;
     public Transform LeftArmIkTarget;
     public Transform RightArmIkTarget;
     public Transform LeftLegIkTarget;
     public Transform RightLegIkTarget;
 
     public Transform Head;
     public Transform UpperTorso;
     public Transform LowerTorso;
     public Transform LeftEye;
     public Transform RightEye;
 
     float ikWeight = 1;
 
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
         {
             StartCoroutine("Walk");
             
         }
     }
     IEnumerator Walk()
     {
 
         float LeftLegMovementHeight = 0.01f;
         float LeftLegMovementWidth = 0.01f;
         float LeftLegMovementSpeed = 2;
 
         yield return new WaitForSeconds(0.1f);
         Debug.Log("LegUp");
         LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition,new Vector3(0,0,LeftLegMovementWidth),Time.deltaTime*LeftLegMovementSpeed);
         yield return new WaitForSeconds(0.1f);
         Debug.Log("LeftForward");
         LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, LeftLegMovementHeight, 0), Time.deltaTime * LeftLegMovementSpeed);
         yield return new WaitForSeconds(0.1f);
         Debug.Log("LegDown");
         LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, 0, -LeftLegMovementWidth), Time.deltaTime * LeftLegMovementSpeed);
         yield return new WaitForSeconds(0.1f);
         Debug.Log("LegBack");
         LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, -LeftLegMovementHeight, 0), Time.deltaTime * LeftLegMovementSpeed);
     }
 
     void OnAnimatorIK ()
     {
         anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, ikWeight);
         anim.SetIKPosition(AvatarIKGoal.LeftFoot, LeftLegIkTarget.position);
 
         anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, ikWeight);
         anim.SetIKPosition(AvatarIKGoal.RightFoot, RightLegIkTarget.position);
 
         anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, ikWeight);
         anim.SetIKPosition(AvatarIKGoal.LeftHand, LeftArmIkTarget.position);
 
         anim.SetIKPositionWeight(AvatarIKGoal.RightHand, ikWeight);
         anim.SetIKPosition(AvatarIKGoal.RightHand, RightArmIkTarget.position);
     }
 
    
 }
 
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

Answer by Perfecter · Jul 08, 2018 at 10:38 AM

Hi. Seems you starts coroutine too many times. Try this:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class IkProceduralAnimation : MonoBehaviour {
      Animator anim;
      public Transform LeftArmIkTarget;
      public Transform RightArmIkTarget;
      public Transform LeftLegIkTarget;
      public Transform RightLegIkTarget;
  
      public Transform Head;
      public Transform UpperTorso;
      public Transform LowerTorso;
      public Transform LeftEye;
      public Transform RightEye;
  
      float ikWeight = 1;
 
     private bool moving = false;
  
  
      // Use this for initialization
      void Start () {
          anim = GetComponent<Animator>();
      }
      
      // Update is called once per frame
      void Update () 
     {
         if (!moving){
             if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
              {
              StartCoroutine("Walk");
              
              }
         }         
      }
      IEnumerator Walk()
      {
           moving = true;
          float LeftLegMovementHeight = 0.01f;
          float LeftLegMovementWidth = 0.01f;
          float LeftLegMovementSpeed = 2;
  
          yield return new WaitForSeconds(0.1f);
          Debug.Log("LegUp");
          LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition,new Vector3(0,0,LeftLegMovementWidth),Time.deltaTime*LeftLegMovementSpeed);
          yield return new WaitForSeconds(0.1f);
          Debug.Log("LeftForward");
          LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, LeftLegMovementHeight, 0), Time.deltaTime * LeftLegMovementSpeed);
          yield return new WaitForSeconds(0.1f);
          Debug.Log("LegDown");
          LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, 0, -LeftLegMovementWidth), Time.deltaTime * LeftLegMovementSpeed);
          yield return new WaitForSeconds(0.1f);
          Debug.Log("LegBack");
          LeftLegIkTarget.localPosition += Vector3.Lerp(LeftLegIkTarget.localPosition, new Vector3(0, -LeftLegMovementHeight, 0), Time.deltaTime * LeftLegMovementSpeed);
          moving = false;
      }
  
      void OnAnimatorIK ()
      {
          anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, ikWeight);
          anim.SetIKPosition(AvatarIKGoal.LeftFoot, LeftLegIkTarget.position);
  
          anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, ikWeight);
          anim.SetIKPosition(AvatarIKGoal.RightFoot, RightLegIkTarget.position);
  
          anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, ikWeight);
          anim.SetIKPosition(AvatarIKGoal.LeftHand, LeftArmIkTarget.position);
  
          anim.SetIKPositionWeight(AvatarIKGoal.RightHand, ikWeight);
          anim.SetIKPosition(AvatarIKGoal.RightHand, RightArmIkTarget.position);
      }
  
     
  }
Comment
Add comment · Show 3 · 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
avatar image Broseph1010 · Jul 08, 2018 at 03:48 PM 0
Share

This is working better, it is running the function correctly, except it is giving extreme values of the localPosition of the object. It going up, left, down, and right should result in it returning to the same localPosition, but it is not. I cannot figure out why. Any help will be greatly appreciated.

avatar image Perfecter Broseph1010 · Jul 09, 2018 at 02:20 PM 0
Share

Seems it happens because you waiting 0.1 sec between steps but using Time.deltaTime in formulas. It can have different values during playing. Check this moment.

avatar image Perfecter Broseph1010 · Jul 09, 2018 at 02:22 PM 0
Share

oh. And you using += to plus the reuslt of Vector3.Lerp. I'm not sure but seems you wanted to use equal (=).

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

98 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

Related Questions

Find a position forward of GameObject with possible angle 1 Answer

How can I set the default position of a 3D object? 2 Answers

How do I make my sound work in proper 3D space for VR 1 Answer

Problem with positioning of scaled models 1 Answer

How would you sort gameobjects by their position in a 3D grid? 1 Answer


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