2D Platformer - Arm rotation / fliping problem
Hello,
I'am creating 2D platformer shooter game as my graduation project. However I came across some bugs which i cannot solve so I would really appreciate someone's help.
So I'am using this script to rotate players arm when aiming and when the hand exceeds 180° character is supposed to flip :
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
if (Mathf.Abs(rotationZ) > 90f && m_FacingRight)
{
Flip();
}
else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight)
{
Flip();
}
if (!m_FacingRight)
{
rotationZ += 180;
}
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
private void Flip()
{
m_FacingRight = !m_FacingRight;
Vector3 theScale = MyPlayer.transform.localScale;
theScale.x *= -1;
MyPlayer.transform.localScale = theScale;
}
}
However I'am also using Flip function in my Character controller script for running left and right. It looks like this :
public void Flip()
{
m_FacingRight = !m_FacingRight;
transform.Rotate(0f, 180f, 0f);
}
Well and my problem is that these two Flip functions overlap.
It is creating bugs like this when facing left and rotating arm :
This is how i would like it to work :
I appreciate any help and advice, thank you.
So, to clarify, do you want the body to always be facing the mouse position or the run direction? Because it sounds like you are trying to do both
Hello,
I created something like Aim state which means my player is ai$$anonymous$$g only when holding right mouse button and he is only able to rotate arm in this state, however I still want him to be able to run correctly while ai$$anonymous$$g. Like ai$$anonymous$$g to the left side of the map and going backwards when moving right.
Answer by DrCox · Jan 08, 2020 at 04:17 PM
Hi, so since an arm rotation >180° is turning your character around, I feel like what you want is the character to just always face the mouse position. Let's say he's running to the right, but the mouse is left. This would mean, he's basically running backwards (might need different animation), pointing the arm to the left, right? If I understood correctly, I would just determine the body direction on whether the Mouse.x is lesser or greater than the Player.x and the arm can then just point at the mouse position.
Hope this helps. Cheers
Hey,
Thanks for your advice, you understood my problem correctly but Im not sure how to implement your solution. What do you mean by $$anonymous$$ouse.x because I'am getting current mouse position in Vector3 type and Vector3 cannot be used as an argument in if statement. I could get Player.x position with "transform.position.x" but what do I execute after the IF ?
Oh, no worries. Let me get more into detail. To deter$$anonymous$$e when to flip your character, you can compare the world position of the mouse with the character position, something like this:
void Update()
{
var mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(mouseWorldPos.x < $$anonymous$$yPlayer.transform.position.x)
{
// mouse is left of $$anonymous$$yPlayer
SetPlayerDirection(-1);
}
else
{
// mouse is right of $$anonymous$$yPlayer
SetPlayerDirection(1);
}
}
void SetPlayerDirection(int x)
{
var currentScale = $$anonymous$$yPlayer.transform.localScale;
if($$anonymous$$athf.Sign(currentScale.x) != $$anonymous$$athf.Sign(x))
{
currentScale.x *= -1;
$$anonymous$$yPlayer.transform.localScale = currentScale;
}
}
You can then remove the flip from you character controller, as the mouse position is the only thing changing the direction of your player.
First of all thank you very much for explaining this to me,
so, I tried it and it kind of fixed the problem since im not using movement flip now however it made arm inverted. You can check in the video in this link.