Scripting for Game developers

Discussion started by duttanemo

Hey guys!!! Created this discussion for people who use CGtrader.com for downloading models and mainly do programming.

Today i am sharing a script to achieve enemy follow AI for ur games

1. First is CharacterMovement.cs

public class CharacterMovement : MonoBehaviour {

public float moveSpeed=10.0F;

void Update () {

//Get Input From User

Vector3 input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));

//Apply Force as per requirement

rigidbody.AddForce (input*moveSpeed); //NOTE:- Applying force in fixed update is recommended for better effect because Physics would be applied at fixedrate gives better results

//To Give a Jump Effect

if (Input.GetKey (KeyCode.Space)) {

rigidbody.AddForce(Vector3.up,ForceMode.Impulse);

}

}

}

2. Second is the FollowScript.cs

public class FollowScript : MonoBehaviour {

public Transform Character; // Target Object to follow

public float speed=0.1F; // Enemy speed

private Vector3 directionOfCharacter;

private bool challenged=false;// If the enemy is Challenged to follow by the player

void Update () {

if (challenged) {

directionOfCharacter = Character.transform.position - transform.position;

directionOfCharacter = directionOfCharacter.normalized;// Get Direction to Move Towards

transform.Translate (directionOfCharacter * speed, Space.World);

}

}

// Will be triggered as soon as player would touch the Enemy Object

voidOnTriggerEnter( Collider other)

{

if (other.CompareTag ("Player")) {

challenged=true;

}

}

}

Friends add the first script is for ur character, second for the enemy. Should work well, also add rigid body and the needfulls

Your answer

In order to post an answer, you need to sign in.

Help