- Home /
MatchTarget and Looping Animations?
Hello,
I'm having an issue with MatchTarget. The same code I have here is working great on interactions where the user hits a button and a single animation is triggered. However, on these where the user holds down the mouse button and then, in the case of pushing/pulling, it doesn't work properly.
For the first of the two under the Update() function, the expected result is for the character to match the target when the mouse is held down and for the first quarter of the "grab animation". However, this only works when I set the start and values to something way higher like 1f and 2f and, in that case, it only works after the first time the mouse button is clicked - never on the first time (I'm assuming because it starts and ends at a value higher than 100% of the animation length?)
The second one, relating to isPushing or isPulling, is a bit different because the animation loops here as the player holds down W or S. How can I make these match during the first loop then stay match for all proceeding loops? Currently I don't see the matching working at all, though, even on the first loop.
My code is below, I hope it's not too messy. I'm still green but I've been really ripping my hair out on this one. The push/pull script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCharacterController;
public class Draggable : MonoBehaviour
{
[SerializeField] int startingMass = 1000;
[SerializeField] int pushingMass = 700;
[SerializeField] float pullSpeed = 0.4f;
[SerializeField] float originalSpeed = 4f;
public GameObject[] objectsToColour;
public GameObject player;
Interacting playerScript;
public Transform objectToMove;
public Transform matchTarget;
public Transform aimTarget;
public bool isGrabbing;
public bool isPushing;
public bool isPulling;
Animator playerAnimator;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
playerScript = player.GetComponent<Interacting>();
objectToMove = this.transform.parent;
playerAnimator = player.GetComponent<Animator>();
}
void Update()
{
IndicateObjectInteraction(); // I've taken this function out below just to not crowd it
if (isGrabbing)
{
print("grabbing matchtarget");
playerScript.MatchTarget(matchTarget, AvatarTarget.RightHand, new Vector3(0, 0, 1), 0f, 0f, 0.25f, aimTarget, true);
}
else if (isPushing || isPulling)
{
playerScript.MatchTarget(matchTarget, AvatarTarget.RightHand, new Vector3(0, 0, 1), 0f, 0f, 0.2f, aimTarget, true);
}
Grab();
PushPull();
if (playerScript.isInActionZone == false)
{
playerScript.isMatchEnabled = false;
this.GetComponent<Draggable>().enabled = false;
}
}
private void Grab()
{
if (playerScript.isInActionZone == this && playerScript.isPlayerFacing && Input.GetButton("Fire1"))
{
isGrabbing = true;
player.GetComponent<vThirdPersonInput>().isHorzDisabled = true;
if (Input.GetButtonDown("Fire1") && isGrabbing)
{
playerScript.isMatchEnabled = true;
playerAnimator.Play("GrabStart");
}
playerScript.isMatchEnabled = true;
playerAnimator.SetBool("isGrabbing", true);
objectToMove.GetComponent<Rigidbody>().mass = pushingMass;
}
else
{
playerAnimator.SetBool("isGrabbing", false);
// playerScript.isMatchEnabled = false;
objectToMove.gameObject.GetComponent<Rigidbody>().mass = startingMass;
player.GetComponent<vThirdPersonInput>().isHorzDisabled = false;
isGrabbing = false;
}
}
private void PushPull()
{
if (isGrabbing && Input.GetButton("Vertical") && Input.GetAxis("Vertical") > 0)
{
isPushing = true;
playerAnimator.SetBool("IsPushing", true);
}
else if (isGrabbing && Input.GetButton("Vertical") && Input.GetAxis("Vertical") < 0)
{
isPulling = true;
playerAnimator.SetBool("IsPulling", true);
objectToMove.parent = player.transform;
objectToMove.gameObject.GetComponent<Rigidbody>().isKinematic = true;
player.GetComponent<vThirdPersonController>().lockRotation = true;
player.GetComponent<vThirdPersonMotor>().freeSpeed.runningSpeed = pullSpeed;
}
else
{
player.GetComponent<vThirdPersonController>().lockRotation = false;
player.GetComponent<vThirdPersonMotor>().freeSpeed.runningSpeed = originalSpeed;
objectToMove.gameObject.GetComponent<Rigidbody>().isKinematic = false;
objectToMove.parent = null;
playerAnimator.SetBool("IsPulling", false);
playerAnimator.SetBool("IsPushing", false);
isPushing = false;
isPulling = false;
}
}
}
Then here is the reference in the "playerScript" where the method is actually housed:
public void MatchTarget(Transform targetPosition, AvatarTarget avatarTarget, Vector3 positionWeight, float rotationWeight, float curveStart, float curveEnd, Transform aimTarget, bool useAim)
{
if (isMatchEnabled)
{
print("matching");
if (useAim)
{
Vector3 targetPostition = new Vector3(aimTarget.position.x, this.transform.position.y, aimTarget.position.z);
this.transform.LookAt(targetPostition);
}
gameObject.GetComponent<Animator>().applyRootMotion = true;
float normalizeTime = Mathf.Repeat(GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).normalizedTime, 1f);
if (normalizeTime > curveEnd)
return;
gameObject.GetComponent<Animator>().MatchTarget(targetPosition.position, targetPosition.rotation, avatarTarget, new MatchTargetWeightMask(positionWeight, rotationWeight), curveStart, curveEnd);
}
}
Thanks so much. If anyone is able to help, that would be appreciated a ton.
Your answer
Follow this Question
Related Questions
How to check Game Build Errors? 0 Answers
Is there a new retargetting system for the animation in 5.5? 1 Answer
Conflicting Animator SetTrigger 0 Answers
Set boolean to false when animation changes? 1 Answer
Stop box collider moving with object 1 Answer