2d platformer flipping problem
Hello community,
so I have a problem for some days now and I cant find a way to solve it. I control the player arm via right stick on controller. If I aim straight up or down, for like a milisecond the arm flips to the opposite side and then back to the desired position. Ingame I hold the button to aim up, flip the Body and the arm goes down for milisecond and up again This bugs me so much and all my attempts like setting the body and arm under another parent didnt work, any idea? Here's the code for my playerscript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour {
public float walkSpeed = 5;
[HideInInspector]
public bool lookingRight = true;
public Transform groundCheck;
public LayerMask whatIsGround;
public float jetpackForce = 20f;
public float jetpackSideForce = 60f;
public Transform arm;
public string horizontalCtrl = "Horizontal_P1";
public string jetpackButton = "Jetpack_P1";
public string rAnalogXMovement = "RAnalogX_P1";
public string rAnalogYMovement = "RAnalogY_P1";
private Rigidbody2D rb2d;
private Animator anim;
private bool isGrounded;
private bool armRight = true;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.15F, whatIsGround);
float x = Input.GetAxis(rAnalogXMovement);
float y = Input.GetAxis(rAnalogYMovement);
float hor = Input.GetAxis(horizontalCtrl);
anim.SetFloat("Speed", Mathf.Abs(hor));
rb2d.velocity = new Vector2(hor * walkSpeed, rb2d.velocity.y);
if (x != 0f || y != 0f)
{
float aim_angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
arm.transform.rotation = Quaternion.AngleAxis(aim_angle, Vector3.forward);
}
if (isGrounded)
{
anim.SetBool("IsGrounded", true);
}
if (!isGrounded)
{
anim.SetBool("IsGrounded", false);
}
/*if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight))
{
Flip();
}*/
if ((x > 0 && !armRight) || (x < 0 && armRight))
{
Flip();
}
bool jetpackActive = Input.GetButton(jetpackButton);
if (jetpackActive)
{
if (lookingRight)
{
anim.SetBool("IsGrounded", false);
rb2d.AddForce(new Vector2(jetpackSideForce, jetpackForce));
}
if (!lookingRight)
{
anim.SetBool("IsGrounded", false);
rb2d.AddForce(new Vector2(jetpackSideForce *-1, jetpackForce));
}
}
}
private void Flip()
{
lookingRight = !lookingRight;
Vector3 myScale = transform.localScale;
myScale.x *= -1f;
transform.localScale = myScale;
//If I am flipping body, flip arms
ArmFlip();
}
private void ArmFlip (){
//Arm flipping
armRight = !armRight;
Vector3 myScale = arm.transform.localScale;
myScale.x *= -1f; //Flip arm on the other side
myScale.y *= -1f; //Flip arm upside-down
arm.transform.localScale = myScale;
}
}
Answer by DuB86 · Jan 30, 2018 at 04:58 PM
sry to bump, but would it help, if I parent it different, say like a gameobject named player, children named arm and body and put that script on body? cant test right now but in theory? or do you see problems with the script?
Your answer
Follow this Question
Related Questions
How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers
2D Weird Jumping 1 Answer
Changing Color 1 Answer