Hi,
My issue involves rotating a 2D player sprite’s z axis to match the slope incline they’re walking on. Essentially I want them to stand up “straight” relative to the ground, rather than the world Y axis, when standing on any point of the inside edge of a circle.
For this I’m trying to use a raycast to return the ground normal and then match the transform.up to this normal.
I wanted to setup a raycast2d which would fire from the base of the player sprite to return new normal data everytime it hits so that when the player moves, they are constantly changing z rotation to match the normal, HOWEVER, the ray seems only to fire until it detects a hit; meaning that it sets the player’s new z axis to whatever normal it first collects, then doesn’t try to check again - despite being in Update(). This obviously leaves the player sprite at an odd and unchanging angle.
Code:
void Update()
{
if (Input.GetAxis("Horizontal") != 0)
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y);
}
if (Input.GetAxis("Horizontal") < -0.1)
{
mySpriteRenderer.flipX = true;
}
if (Input.GetAxis("Horizontal") > 0.1)
{
mySpriteRenderer.flipX = false;
}
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.down, 5.0f);
// Debug.DrawRay(origin, Vector2.down, Color.red, hit.distance);
transform.up = hit.normal;
}
Please can anyone help?
(I’m pretty new to this so apologies if this is a ridiculous question for whatever reason :L)