Question by
I-Watcher · Oct 21, 2020 at 10:59 AM ·
localscaleturn
I want to turn my character left and right with local scale.
My character is move by click on screen. This is my script. The Turn function is not working because my character is keeping flipping left and right when I change its position. public float speed; public LayerMask clickMask; public float maxDistance;
Vector3 target;
bool isMoving = false;
Animator animator;
bool turnLeft;
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 clickPosition = -Vector3.one;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxDistance, clickMask))
{
clickPosition = hit.point;
target = new Vector3(clickPosition.x, transform.position.y, transform.position.z);
}
isMoving = true;
}
MoveToTouchPos();
Turn();
if (turnLeft == true)
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
}
void MoveToTouchPos()
{
if (isMoving==true)
{
var step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, step);
animator.SetBool("isWalking", true);
}
if (transform.position.x==target.x)
{
animator.SetBool("isWalking", false);
}
}
void Turn()
{
if (target.x < transform.position.x)
{
turnLeft = true;
// transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
if (target.x > transform.position.x)
{
turnLeft = false;
//transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
}
Comment
Your answer
Follow this Question
Related Questions
can i create a grid, using the screen size? 2 Answers
[Beginner] AutoResolve Combat based on Level 0 Answers
Scale object when player is within a given distance of it 1 Answer
Saving default scale of an object c# 1 Answer
LocalScale not working? 4 Answers