- Home /
Enemy/Partner AI without rotation
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.
Have you tried Vector2.$$anonymous$$oveTowards? For the distance between the objects use distance formula. For right or left side check if x pos is greater or less than mario.
$$anonymous$$eep in $$anonymous$$d, I'm using 2d sprites on a 3d surface, so I don't think I can use Vector 2.
So your gameplay is similar to that of castle crashers were you can move front to back and side to side but your character appears 2D?
Answer by fuego_see_money · Jul 23, 2015 at 09:15 PM
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)
}
}
}
}
This had one oddball effect. He would always be facing away from me, even when I set it to negative, and wouldn't actually move. He would just start flashing, and once I got to a certain point, he would appear in front of me, still flashing. I wish I could explain it more, but if you get the time, please test it.
Really? Is your game 2-D? What are your global axis?
You are using 2D sprites in a 3D environment, like Paper $$anonymous$$ario, correct?
Also what value were you setting to negative?
Yes, I said that.
I am setting the x-scale to negative, because that's how it is in game for me. When I make sprites (slice or normal) they appear backwards in game, even before your script, so in order to set them right, I reverse the x-scale.
Your answer
Follow this Question
Related Questions
Send an enemy back to its spawn point using waypoints 2 Answers
My bullet has no velocity after it is spawned. 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
Enemy AI C# 0 Answers
Multiple Cars not working 1 Answer