I have tried a ton of AI scripts around here for my game, and they all have one thing in common. They all rotate the object it’s attached to.
I’m trying to make a Paper Mario game, and if anyone has played those games, you know that no-one (ok, one star kid), turns sideways when the camera is facing them.
I need to know how to make a script that follows the player and stops at a distance, and depending if he/she is on the right or left side of mario, their x-axis scale is inverted.
Right now all I’m getting is just a bunch of rotating sprites, and that isn’t good for a paper mario game.
try this, keep in mind I am at work and cannot test anything. let me know if you need help still after this. attach this to your partner. its commented fairly well so I hope you understand it.
public class Partner : MonoBehaviour
{
public GameObject toFollow;//this should be Mario,set in inspector
public float stoppingRadius = 5f;//however much you want
public float moveSpeed = 2f;//however much you want
public float initXScale;
void Start()
{
initXScale = this.transform.localScale.x;
}
void FixedUpdate()
{
//get direction towards mario, multiply by your follow speed
Vector3 dir = toFollow.transform.position - this.transform.position;
float distance = dir.magnitude;
dir.Normalize();
//are we far enough away from him?
if(distance > stoppingRadius)
{
//move towards him
//get move vector
Vector3 move = dir * moveSpeed;
//I am going to assume you dont want your partner to move on the up axis (float away) so we are going to remove the Y component of move
move = new Vector3(move.x, 0, move.z);
//I dont know if youre using a rigidbody...im going to just assume you arent
this.transform.position += move;
//finally...are we on the left or right of him?
if(dir.x > 0)
{
this.transform.localScale = new Vector3(initXScale, this.transform.localScale.y, this.transform.localScale.z)
}
else
{
this.transform.localScale = new Vector3(-initXScale, this.transform.localScale.y, this.transform.localScale.z)
}
}
}
}