Enemy AI Script Error
Hey guys,
Currently I'm trying to create a enemy AI that follows the player once it hits the raycast. However I'm getting 3 errors on my script. I don't know how to fix them, if anyone could help that would be great, thank you in advance.
The line of code that I'm getting an error with is the very last one, with the GetComponent()
// Update is called once per frame void Update () { Player = GameObject.Find ("Player").transform.position;
Xdif = Player.x - transform.position.x;
Ydif = Player.y - transform.position.y;
playerDirection = new Vector2 (Xdif, Ydif);
if (!Physics2D.Raycast (transform.position, playerDirection, 5, Wall)) {
GetComponent<Rigidbody2D>().AddForce (playerDirection.normalized = speed);
}
}
GetComponent().AddForce (playerDirection.normalized = speed);
In this line I think you need to replace "=" with "*" . And you are getting this error because Vector3.normalized is a read only ,you can not assign any value to vector3.normalized.
hope you are looking for this. Thanks
Answer by Abhiroop-Tandon · Mar 25, 2016 at 10:12 AM
This line GetComponent<Rigidbody2D>().AddForce (playerDirection.normalized = speed);
doesnt makes sense, you are missing something there like "" or "". The error thats coming is, as @MehulRughani said because Vector3.normalized is a read only and returns the normalized vector so you cannot assign a return function for obvious reasons