- Home /
Procedural Squash and Stretch - Trying to convert code from ActionScript example
Hi, I found this example of creating procedural squash and stretch in ActionScript. I am trying to replicate a similar effect in unity. The basic idea is to have a sprite (or in my case a quad mesh) squash and stretch based on the velocity of the object. The problem I am having is the ActionScript example uses a transformation matrix and based on my research (and very limited knowledge) using a transformation matrix in unity seems like a bad idea -- or I am just lazy. My results are not quite there yet. I can get my object to squash and stretch but it seems to do so perpendicular to the direction the object is traveling. Also I have a problem with the object flipping orientation. Long story short, my question is how can I fix this, or is there an example of doing something similar in Unity?
using UnityEngine;
using System.Collections;
public class SquashPlayer : MonoBehaviour {
public float intensity;
private Rigidbody2D rb;
private float velX;
private float velY;
private float squash;
private float xScale;
private float yScale;
private float originalScale;
private Quaternion directionTraveling;
void Start ()
{
intensity = 1;
rb = GetComponent<Rigidbody2D>();
originalScale = transform.localScale.x;
}
void FixedUpdate ()
{
velX = Mathf.Abs (rb.velocity.x);
velY = Mathf.Abs (rb.velocity.y);
squash = ((velY + velX) / intensity);
yScale = ((squash / -2) + originalScale);
xScale = (squash + originalScale);
directionTraveling = Quaternion.LookRotation(rb.velocity);
transform.rotation = directionTraveling;
transform.localScale = new Vector3(xScale ,yScale , 1);
}
}
Your answer
Follow this Question
Related Questions
stretch and squash animation overlapping with blocks 0 Answers
How to achieve squash and stretch on characters similar to 3D Mario games 0 Answers
How do I have a animation work on top of a joint relationship? 0 Answers
Will humanoid rigs ever support stretch? 0 Answers
Can the animation editor create local rotational data? 3 Answers