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.

you may want to try this

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.

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.