- Home /
An NPC "moonwalks"
As part of my newly started education in gamedevelopment I encoutered a peculiar issue: we have each progammed an NPC so when the player came within a certain distance of the NPC, the NPC would react by moving towards the player. However, in my project, the NPC "moonwalks" away from the player, a development which have left my teacher stumped.
Here is the code in question, the programming language is C#:
using UnityEngine;
using System.Collections;
public class Troll : MonoBehaviour {
Animation anim;
GameObject player;
// Use this for initialization
void Start () {
anim = GetComponent<Animation> ();
anim.Play ("Idle_02");
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update (){
if (Vector3.Distance (transform.position, player.transform.position) < 3) {
Vector3 dir = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt (dir);
anim.Play ("Attack_01");
rigidbody.velocity = new Vector3 (0, rigidbody.velocity.y, 0);
} else if (Vector3.Distance (transform.position, player.transform.position) < 20) {
Vector3 dir = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt (dir);
anim.Play ("Run");
rigidbody.AddForce (transform.forward * 100);
} else {
anim.Play ("Idle_01");
}
}
}
An odd detail is that if I change "rigidbody.AddForce (transform.forward * 100);" to a negative number, the NPC will move forward. The NPC is the "Earthborn Troll" from the asset store, in case that is an important detail.
I would greatly appreciate any help.
It works in a newly created project with just that script, a cube to make the floor, a cube with this script and a FirstPersonController with the tag Player.
Check your project for what it might be changing the behaviour. If at last you don't find what it is and you just have this in your proejct, start again with a clean proejct.
Answer by bubzy · Sep 29, 2014 at 09:41 AM
you may want to try this
http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
if you add a force to the rigidbody, you are going to have to slow it down too, while this may work well for solid objects, an NPC may suffer from it, unless of course you want them to have some sort of inertia. as for why your code doesn't work, it may be worth checking out the actual direction the model is looking in (not what it looks like hes looking at).
hope this helps a little bit.
additionally, as an aside, i (personally) would use variables for the attack distance like
float attackDistance = 3f;
float awareDistance = 20f;
it may also be worthwhile looking at a state$$anonymous$$achine for the ai as it is possible that both of your conditions may be met.
Answer by JHM007 · Sep 30, 2014 at 11:26 AM
Thank you all for the input, but as it turns out, the fault was my own and rather... stupid. I had mistaken "Freeze Position" with "Freeze Rotation" under "Rigid Body".
So thanks again for the help.
Your answer
Follow this Question
Related Questions
I can do this in JS or not ??? 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Adding value to GameObject[] 1 Answer