- Home /
Move Torwards Transform.Localscale not working
so i have made a simpel script that makes an enemy move torwards the player it works really well i just have a litle problem which is when i try to Transform.local scale my enemy so that the graphic will flip if hes changing direction it simply dosent work im using the same code that i used to flip my player when i press a A/D to move Right/Left by simply checking wether the Rigidiy body is moving right or left no idea why its not working neither did anyone that i asked and not quite sure what to google but gave a shot with no luck any help is appreciated Ty in advance.
Source Code
public bool EnemyOnPlayer;
private Rigidbody myrigidbody;
public PlayerController thePlayer;
// Use this for initialization
void Start () {
EnemyOnPlayer = false;
myrigidbody = FindObjectOfType<Rigidbody> ();
thePlayer = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
if (EnemyOnPlayer == false)
{
transform.position = Vector3.MoveTowards (transform.position, thePlayer.transform.position, Time.deltaTime * move_speed);
}
if (myrigidbody.velocity.x > 0)
{
transform.localScale = new Vector3 (1f, 1f, 1f);
}
if (myrigidbody.velocity.x < 0)
{
transform.localScale = new Vector3 (-1f, 1f, 1f);
}
}
Answer by Gardes · Jun 02, 2015 at 05:01 AM
Vector3.MoveTowards is not adding any physic things, so velocity stays always at 0.
If you still want to move your Object without physics just calculate with your positions.
if (transform.position.x < thePlayer.transform.position.x)
{
transform.localScale = new Vector3 (1f, 1f, 1f);
}
if (transform.position.x > thePlayer.transform.position.x)
{
transform.localScale = new Vector3 (-1f, 1f, 1f);
}
This is untested pseudo code, just to give you a shot.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i get the LocalSize of an object? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Crouch Script/ Scaling transform from one side only 1 Answer