Help apdapting character flip script to use GetComponent().flip = true;
Hello! Thanks in advance for the help and my apologies for a probably pretty newbie question but been struggling to figure this out properly. I've been learning how to make character controllers for a platformer and it's been going good but I am a much better artist and animator than a programmer. Need some help adapting the movement script to use GetComponent<Puppet2D_GlobalControl>().flip = true;
as my character flipping method.
I am using a skeletal animation solution called Puppet2d and in order for flipping assets to work properly I must use that component I mentioned above, otherwise the bone positioning gets screwed up.
Here's my script below before I add the component to it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spacemancontroller : MonoBehaviour {
//how fast the character can move
public float topSpeed = 10f;
// tell the sprite which way it is facing
bool facingRight = true;
//physics in fixed update
void FixedUpdate()
{
//get move direction
float move = Input.GetAxis ("Horizontal");
//add velocity to the rigidbody in the move direction * our speed
GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
//if were facing the neg direction and not right, flip
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
}
void Flip()
{
//saying we are facing the opposite directions
facingRight = !facingRight;
//get the local scale
Vector3 theScale = transform.localScale;
//flip on the x axis
theScale.x *= -1;
//reapply that to the level scale
transform.localScale = theScale;
}
}
I've been reading over the script trying to make sure I understand exactly what's happening at each line but I think I'm misunderstanding part of it because when I replace the instances of Flip();
with GetComponent<Puppet2D_GlobalControl>().flip = true;
I can get the character to properly turn around once but I can't get him to turn around again.
Thanks again for any help you guys can provide, I've been sitting on this for many days without any luck.
Answer by wolfkillshepard · May 03, 2017 at 12:19 AM
Thanks for the reply! That makes sense, I'll change that and see if I can get it working when in back home in a couple hours. I'll try caching it too after I get it working.
Update, Solved:
I changed my if statement to use:
Puppet2D_GlobalControl _globalControl = gameObject.GetComponent<Puppet2D_GlobalControl> ();
if (Input.GetAxis ( "Horizontal" ) < 0)
{
_globalControl.flip = true;
}
else if (Input.GetAxis ("Horizontal" ) > 0)
{
_globalControl.flip = false;
}
character now flips back and forth just fine. Anyone using puppet2D would need to do something like this for their flipping control.
Answer by Jawchewa · May 03, 2017 at 12:15 AM
I'm not really experienced with Puppet2D, but from what your describing, I think you'll want to replace GetComponent<Puppet2D_GlobalControl>().flip = true;
with GetComponent<Puppet2D_GlobalControl>().flip = !GetComponent<Puppet2D_GlobalControl>().flip;
From what you were describing, it sounds like you were always setting the boolean value of flip to true, instead of toggling the value. Also, unrelated, but I would probably recommend caching the Puppet2D_GlobalControl component in a variable, so that you don't have to keep calling GetComponent.