Too subjective and argumentative
Rotation stops OnTriggerStay2D
So, I have a character that turns to the right or left depending on where the mouse is on the screen, and when they're turned right, OnTriggerStay2D works for a second and then stops. I have no idea what is causing this. Here is my code for the player and the OnTriggerEnter2D script:
region variables
public float jumpHeight = 5; public float moveSpeed; public float maxSpeed; public Rigidbody2D rb; #endregion variables
void Update () {
//float horizontal = Input.GetAxis("Horizontal") * moveSpeed;
//rb.AddForce(transform.right * horizontal);
if (Input.GetKeyDown(KeyCode.Space) && rb.velocity.y == 0)
{
rb.AddForce(transform.up * jumpHeight);
}
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
Vector2 mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if (mouse.x < Screen.width / 2)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
#region movement
if (Input.GetKey("d"))
{
rb.AddForce(-transform.right * moveSpeed);
}
if (Input.GetKey("a"))
{
rb.AddForce(transform.right * moveSpeed);
}
if (Input.GetKeyUp("d"))
{
rb.velocity = Vector2.zero;
}
if (Input.GetKeyUp("a"))
{
rb.velocity = Vector2.zero;
}
#endregion movement
}
if (mouse.x > Screen.width / 2)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
#region movement
if (Input.GetKey("d"))
{
rb.AddForce(transform.right * moveSpeed);
}
if (Input.GetKey("a"))
{
rb.AddForce(-transform.right * moveSpeed);
}
if (Input.GetKeyUp("d"))
{
rb.velocity = Vector2.zero;
}
if (Input.GetKeyUp("a"))
{
rb.velocity = Vector2.zero;
}
#endregion movement
}
public float speed = 10; public Transform player; public Transform elevator; public Transform starship; public int floorLevel = 4;
void OnTriggerStay2D (Collider2D c)
{
Debug.Log("testing testing 123 can you hear me let me see");
if (c.tag == "Player")
{
player.parent = elevator;
#region down
if (Input.GetKeyDown("s") && floorLevel == 1)
{
Debug.Log("You can't go down any further!");
}
if (Input.GetKeyDown("s") && floorLevel == 2)
{
floorLevel = 1;
}
if (Input.GetKeyDown("s") && floorLevel == 3)
{
floorLevel = 2;
}
if (Input.GetKeyDown("s") && floorLevel == 4)
{
floorLevel = 3;
}
#endregion down
#region up
if (Input.GetKeyDown("w") && floorLevel == 4)
{
Debug.Log("You can't go up anymore!");
}
if (Input.GetKeyDown("w") && floorLevel == 3)
{
floorLevel = 4;
}
if (Input.GetKeyDown("w") && floorLevel == 2)
{
floorLevel = 3;
}
if (Input.GetKeyDown("w") && floorLevel == 1)
{
floorLevel = 2;
}
#endregion up
}
}
void OnTriggerExit2D ()
{
player.parent = starship;
}
Good day,.
How you detect the trigger collision? Do you have a collider in your player?. $$anonymous$$ake sure that when you rotate, the collider is not rotating with a distant pivot point, so is going outside the area of collision
Hi, First, I detect trigger collision using collides and rigid bodies (all 2D). Second, I rotated the character at their y axis, only flipping it to 180 and 0 depending on where the mouse is. And third, I fixed the problem by having my character rotate 360 degrees rather than 0 degrees. I'm not sure how that fixed it, but it did. It seems it only works past 180 degrees, any explanation would be greatly appreciated.
Answer by jmoocow2003 · Apr 23, 2018 at 09:20 PM
Fixed it by making the character rotate to 360 degrees rather than to 0 degrees, because the OnTriggerStay2D seems to only work when your sprite is rotated past 180 degrees. I have no idea why this works, but it did, and it produces the same effect. Any explanation would be greatly appreciated, thanks.
Follow this Question
Related Questions
Quaternion.FromToRotation - is it a bug? Possible workarounds? 1 Answer
When exporting from Blender, animation gets messed up 0 Answers
Very basic rotation BUG. transform.up = transform.up sets Y rotation to 0. 1 Answer
2d Animation, Rotating GameObject bug 1 Answer
Trouble with rotating player when gravity is shifted 0 Answers