- Home /
NPC that follows me tilts backwards as he get too close to me.
Hi guys!
Hope someone can help me out. I have a C# script that allows an NPC to follow me around and stop at a certain distance.
If the NPC gets too close he will tilt back until horizontal. I have a feeling i need to set y axis to 0 but not entirely sure at what point or how in the script. Below is a sample of my code, if anyone can help that would be great!
public class NPCFollow : MonoBehaviour
{
public GameObject player;
public GameObject npc;
public float followspeed;
public int alloweddistance = 1;
public float targetdistance;
public RaycastHit Shot;
private Animator anim;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
transform.LookAt(player.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
targetdistance = Shot.distance;
if (targetdistance >= alloweddistance)
{
followspeed = 0.02f;
npc.GetComponent<Animator>().SetBool("isIdle", false);
npc.GetComponent<Animator>().SetBool("isWalking", true);
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, followspeed);
}
else
{
followspeed = 0;
npc.GetComponent<Animator>().SetBool("isIdle", true);
npc.GetComponent<Animator>().SetBool("isWalking", false);
}
}
}
Answer by cmills95 · Jul 18, 2020 at 02:34 PM
@Compii I found that if I added
var rot = transform.eulerAngles;
rot.x = 0;
transform.eulerAngles = rot;
It sorted it right out for me! Thanks for your input!
Nice! I think you could shorten that a bit by just doing:
transform.eulerAngles.x = 0;
Could save some meomry, even though that probably is not important for you.
If it works that short I'll use it for sure! Every little helps man!
Now to get these headache NPC's to walk up the hill rather than using their idle animation and sliding like weirdos. Otherwise everything else is great!
Answer by Compii · Jul 18, 2020 at 11:07 AM
Since you're doing all this in Update you could set the x and z rotation to 0 which I think should fix your problem. However I think it would be a much better way to remove the origign of the rotation instead of adding something to work against it, but I can't see anything causing this, the only thing I could think about is that there might be something wrong with your Idle Animation because you say that the NPC tilts if he gets too close which seems to be around the time when the Idle Animation is set to true.
Do you know how i would remove the origin? I found that this part is responsible for what is happening...
transform.LookAt(player.transform);
I found a way around using a Nav$$anonymous$$esh script but it makes the animations look quite unnatural. Here's the Nav$$anonymous$$esh code I was using.
public class NPCFollow2 : $$anonymous$$onoBehaviour
{
public Transform transformToFollow;
public GameObject npc;
public float alloweddistance = 5;
public float targetdistance;
private RaycastHit Shot;
Nav$$anonymous$$eshAgent agent;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<Nav$$anonymous$$eshAgent>();
}
// Update is called once per frame
void Update()
{
agent.destination = transformToFollow.position;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
targetdistance = Shot.distance;
if (targetdistance >= alloweddistance)
{
npc.GetComponent<Animator>().SetBool("isIdle", false);
npc.GetComponent<Animator>().SetBool("isWalking", true);
}
else
{
npc.GetComponent<Animator>().SetBool("isIdle", true);
npc.GetComponent<Animator>().SetBool("isWalking", false);
}
}
}
}
Thanks for your responce.
I thought the Origin was something unwanted, but LookAt does what its supposed to. You could of course go all the way around LookAt and calculate yourself how the NPC needs to be rotated to look at you but all of that would be way more complicated than just setting the x/z rotation to zero(or the value it was before), which i would recommend. I cant help you with your Navmesh however because I dont know anything about that.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
NPC does not follow player movement 0 Answers
Multiple Cars not working 1 Answer
Camera Tilt Smooth [Help Needed] 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer