Hi I’m trying to make a movement system like forager where my character faces right when my mouse is on the right half of the screen and when it moves to the left half of the screen my character will turn and look left this has been a huge challenge and I just can quite get it right. I don’t need it to follow the mouse always in a circle or up or down just flip right to left when mouse is on the corresponding side of the screen. Thanks so much
public class PlayerMovemk4 : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
private Vector2 MoveAmount;
private bool FacingRight = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
MoveAmount = moveInput.normalized * speed;
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 10);
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + MoveAmount * Time.fixedDeltaTime);
}
}
public class PlayerMovemk4 : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
private Vector2 MoveAmount;
private bool FacingRight = true;
//use this if you will flip just the sprite
//SpriteRenderer sr;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
//use this if you will flip just the sprite
//sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
MoveAmount = moveInput.normalized * speed;
/*
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 10);
*/
//find mouse position and transform it into the world position
Vector3 mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
//and compare it with the needed "middle" point then flip if it is needed
//here the player is the "middle"
if (mousePos.x < transform.position.x && FacingRight)
{
Flip();
}
else
if (mousePos.x > transform.position.x && !FacingRight)
{
Flip();
}
//you can take the middle of the screen too
/*
float middle = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0)).x;
if (mousePos.x < middle && FacingRight)
{
Flip();
}
else
if (mousePos.x > middle && !FacingRight)
{
Flip();
}
*/
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + MoveAmount * Time.fixedDeltaTime);
}
void Flip ()
{
//I think in the C# you can't do transform.localScale.x = - transform.localScale.x;, also do next:
Vector3 tmpScale = transform.localScale;
tmpScale.x = - tmpScale.x;
transform.localScale = tmpScale;
FacingRight = !FacingRight;
//or you can flip only the sprite in the sprite renderer
/*
sr.flipX = !sr.flipX;
FacingRight = !FacingRight;
*/
}
}
Dang man you have helped me out again thanks so much I will send you a build when I finish