Hi guys.
I have a problem with my 2d game’s main mechanic. The goal of my game is to reunite a herd of elephants in a puzzle environment.
The player starts as one elephants and tilts the device to move the elephant. When another elephant is encountered the player has the option to add that elephant to the herd.
When this happens, the collected elephant locks onto the back of the player or the last elephant in the line, the line of elephants then move as one unit.
My player elephant has a flip function which flips the sprite when the player needs to travel in the opposite direction. Here is my flip function -
void Flip()
{
facingRight= !facingRight;
Vector3 theScale= transform.localScale;
theScale.x *= -1;
transform.localScale= theScale;
}
So far, my collected elephant locks onto the back of the player using HingeJoint2d, however when my player elephant flips, the collected elephant continues to flip repeatedly and rapidly then goes back to normal when facing to the right again.
I’ve copied the flip function to my collectible elephant and currently trying to get it to work properly. here is the code that I’ve been playing with but i’m having no luck -
this is on my collectable elephant (as its a different game object to the player)
public class elephantMove : MonoBehaviour {
public playerMove playerRight;
bool elefacingRight= true;
private float elemove;
Animator anim;
// Use this for initialization
void Start () {
anim= GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
elemove = transform.position.x;
anim.SetFloat ("eleSpeed", Mathf.Abs(elemove)); //set the Speed parameter to the value after the comma
if(GetComponent<HingeJoint2D>().enabled == true){ // if move is more than 0 and the player is NOT facing right
if(elemove++ && !playerRight.facingRight) // if move is more than 0 and the player is NOT facing right
eleFlip(); //see flip below
else if(elemove-- && playerRight.facingRight) //if move is less than 0 and facing right
eleFlip(); //see flip below
elephant now has its own move funtion.
}
}
void eleFlip()
{
elefacingRight= !elefacingRight;
Vector3 theScale= transform.localScale;
theScale.x *= -1;
transform.localScale= theScale;
}
}
So, when my player elephant flips, I want the collected elephant to flip as well, so I’ve called the player direction in the if statements on line 28 and 30. I’ve also made a move variable which is the x position of the collected elephant. I’ve also said in the if statement that is the x position increases and decreases, carry out the flip function.
This is giving me errors though, saying that these cannot be used together.
Any help will be appreciated here guys, i’m stuck and don’t know what else to try.
Sorry for the long post.
Mark