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