I am attempting to make the sprite move by clicking a location and travelling to it. The current code I’m using for that worked before I implemented the next step. Next I wanted to add animation and it somewhat worked. However I cannot get my sprite to flip or reverse so it faces the way I clicked? Thanks in advance!
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Move : MonoBehaviour
{
public float speed;
private Vector2 targetPosition;
private bool facingLeft = true;
private Rigidbody2D rb;
private Animator anim;
void Start()
{
targetPosition = new Vector2(0.0f, 0.0f);
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPosition = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(targetPosition.x, targetPosition.y, 0.0f));
}
this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
}
void reverseImage()
{
facingLeft = !facingLeft;
Vector2 theScale = rb.transform.localScale;
theScale.x *= -1;
rb.transform.localScale = theScale;
}
void FixedUpdate()
{
float h = Input.GetAxis(targetPosition.x);
if (h < 0 && !facingLeft)
reverseImage();
else if (h > 0 && facingLeft)
reverseImage();
}
}
Did you check FlipX and FlipY ?
Would something like this be what you’re looking for? I moved the flipping code out of fixedUpdate because I would think you’d only want to call that once when you are deciding the new position
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPosition = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(targetPosition.x, targetPosition.y, 0.0f));
//moving to the right
if(targetPosition.x - this.transform.position.x > 0)
{
theScale.x = 1;
rb.transform.localScale = theScale;
}
//moving to the left
else if(targetPosition.x - this.transform.position.x < 0)
{
theScale.x = -1;
rb.transform.localScale = theScale;
}
}
this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
}
Edit: because targetPosition.x is in world space, you need to compare it to the players position in world space, hence the targetPosition.x - this.transform.position.x statements
1 Like
TheOnlyMike675 theScale is producing an error for me. Do you know why? And if you do, do you know how to solve it?
VishwasGragani I have a way of flipping, I just dont understand how to implement it
Yes, my bad, need to create theScale variable before the function. Because I removed the other two it wasn’t included this time. Try this!
Vector3 targetPosition;
Vector3 theScale = new Vector3(1f, 1f, 1f);
float speed;
Rigidbody2D rb;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPosition = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(targetPosition.x, targetPosition.y, 0.0f));
//moving to the right
if(targetPosition.x - this.transform.position.x > 0)
{
theScale.x = 1;
rb.transform.localScale = theScale;
}
//moving to the left
else if(targetPosition.x - this.transform.position.x < 0)
{
theScale.x = -1;
rb.transform.localScale = theScale;
}
}
this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
}
1 Like
It works perfectly! Thank you so much!
1 Like