- Home /
The question is answered, right answer was accepted
2D Sprite Scroller Shooter - Face Right When Standing still
I'm quite new to coding/Unity and I'm making a horizontal scroller shooter for an school project. My Player animations were all facing direction of movement just fine until I changed x-axis movement in input manager to joystick instead of wasd. Now the Player faces left on default even if I was just moving right.
Is there an easy band-aid fix for this? It's just for a demo, I'm an artist not coder, so if I could insert a line of code that makes the player face right when not moving that would be perfect.
My code is below :)
// Use Start for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
InvokeRepeating ("Shoot", 0.0f, 1.0f / bulletsPerSecond);
//this is a timer for 5x a second we are calling the function "shoot"
}
void Shoot(){
canShoot = true;
}
// Update is called once per frame
void Update(){
if (Input.GetButton ("Jump") && canShoot) {
Vector2 spawnPoint = new Vector2 (transform.position.x, transform.position.y);
Rigidbody2D bulletInstance = Instantiate (bulletPrefab, spawnPoint, transform.rotation) as Rigidbody2D;
bulletInstance.AddForce (transform.right * bulletSpeed);
canShoot = false;
}
// X axis
if (transform.position.x <= -7.61f) {
transform.position = new Vector2(-7.61f, transform.position.y);
} else if (transform.position.x >= 20f) {
transform.position = new Vector2(20f, transform.position.y);
}
// Y axis
if (transform.position.y <= -0.84f) {
transform.position = new Vector2(transform.position.x, -0.84f);
} else if (transform.position.y >= -0.01f) {
transform.position = new Vector2(transform.position.x, -0.01f);
}
}
void FixedUpdate ()
{
xSpeed = Input.GetAxis ("Horizontal") * speed;
ySpeed = Input.GetAxis ("Vertical") * speed;
rb.velocity = new Vector2 (xSpeed, ySpeed);
}
Hey mate, maybe I'm not seeing it here but where are you flipping your sprite? Are you manually flipping the scale somewhere or do you have a move left animation and a move right animation?
If so you must be setting the scale or a float in your animator somewhere.
I have a move left and a move right animation! I ended up figuring out, I just changed the horizontal value of "dead" to -1 in the input manager and that fixed it! Not quite sure why but hey pleased it worked! Thanks anyway!
Follow this Question
Related Questions
Stop Moving When Not Touching Screen? 1 Answer
Rotating an object towards target on a single axis 2 Answers
Sprite moving too far/fast 1 Answer
Problem when rotating sprite and moving it foward 1 Answer
Sprite is not shown moving 1 Answer