Flip character on Collision?

Morning All,

So I have this 2d character, an animated sprite, when he collides with something, I want him to flip around. Normally, not a problem as I could flip the TK2d texture in X, BUT my sprite has a child object animated fin. This introduces pivot issues with the fin as well as the child sprite doesn’t just flip with its parent. So I just added a 2nd fin to the backside and decided to rotate him 180 in y on collision.

In the picture I attached a a script with an OnTriggerEnter() command to the wall on the right and it flips the fish nicely.

 void OnTriggerEnter (Collider other)
{ 
 other.transform.eulerAngles = new Vector3(0,180,0);	
}

What I think I need is generic event that flips the current Y value whenever he hits a collider with this script, I’m just not sure how to do it.
Any help would be mighty appreciated.

Try, transform.localScale.x

yeah… that will work to flip the artwork. What I liked about flipping the actual rotation was it also made the fish swim in the other direction as well, as his forward swim motion was in another script. I could use a GetComponent and multiply the speed variable in his swim script by -1 like this:

void OnTriggerEnter (Collider other)
	{
		Vector3 temp = other.transform.localScale;
		if(temp.x == 1)
		{
			temp.x = -1;
			Swim swimScript = other.GetComponent<Swim>();
			swimScript.myDirection = -1;
		}
		else if(temp.x == -1)
		{	temp.x = 1;
			Swim swimScript = other.GetComponent<Swim>();
			swimScript.myDirection = 1;
		}
		other.transform.localScale = temp;
	}

But the pivot on the fin is not corrected (see player below).

https://dl.dropboxusercontent.com/u/1413577/Swim-test/Swim-test.html

Flipping the rotation of the sprite’s hierarchy seemed to work, but would something like the above code be the cleanest way to ‘toggle’ him to reverse direction?

thanks,

B.

Hi outtoplay !

You can make your code a little better by removing the if and else statement like this :

void OnTriggerEnter (Collider other) {
     Vector3 temp = other.transform.localScale;

     temp.x = -temp.x ;
     Swim swimScript = other.GetComponent<Swim>();
     swimScript.myDirection = -swimScript.myDirection;

     other.transform.localScale = temp;
}

or you can also do :

void OnTriggerEnter (Collider other) {

     Swim swimScript = other.GetComponent<Swim>();
     swimScript.myDirection = -swimScript.myDirection;

     other.transform.localScale = new Vector3(-other.transform.localScale.x, other.transform.localScale.y, other.transform.localScale.z);
}

so you don’t have to create a temporary variable of the localScale.

Can you please send me the script you use to move the fin so maybe I’ll be able to explain you why the rotation of the fin isn’t working properly.

Sorry for my bad English.

V.

V,

Thanks so much for the help. I was trying to get that ‘toggle’ type of setup, but wasn’t sure how. I really appreciate it.

As for the fin, there is no script on it. It’s just a simple sprite, parented to a empty GameObject which is animated with a ping pong animation curve(so I could set the pivot where you’d want it), and that is parented to the Fish Body sprite. Like this:

fish
…/gameobject (animated)
…/fin sprite

If I parent the fin directly to the fish body, it flips correctly, but it’s pivot is in the center of the fin and looks wrong. I wonder if I can offset a pivot on a sprite created with TK2D? If I could animate the fin directly, it would work fine. Or if there is a way to propagate the x.localscale down to the fin…hmm.

Ok so I looked through my unity projects and I decided to create a little project to simulate the fin rotation. So I created a big and a small arrow to simulate the fish and the fin to see if I could make something better about the fin rotation. First of all, the pivot point of the fin bug as anyone can see in your demo, and I found out it was because of the Hierarchy :

In fact when you set the localScale.x to something negative (in this case -1), everything bug in the animation.
So you have to set this hierarchy :

fish

AND

gameobject (animated)
…/fin sprite

The gameobject (animated) and the fin need not be the children of the fish

In your script, you have to had serveral lines to also apply the rotation to the fin :

public GameObject animator;     //This will have to be assigned in the Inspector to the empty GameObject with the ping-pong animation.

void OnTriggerEnter (Collider other) {
     Vector3 temp = other.transform.localScale;
     Vector3 temp2 = animator.transform.localScale;    //Animator localScale

     temp.x = -temp.x ;
     temp2.x = -temp2.x;     // Set the rotation
     Swim swimScript = other.GetComponent<Swim>();
     swimScript.myDirection = -swimScript.myDirection;
 
     other.transform.localScale = temp;
     animator.transform.localScale = temp2 ;     // Apply the rotation
}

Unfortunately, because the fin isn’t child of the fish anymore, you have to do a little script to make the fin follow the fish, but it isn’t really hard and I can do it if you want me to do it.

Bye,

V.