x


How to make basic AI in a 2d game?

Hello:

How can i make a basic AI such as in games like Super Mario Bros >> what i mean making the enemy moving from left to right and Right to left.

any script for that please.

if my English was bad ((forgive me)).

more ▼

asked Jun 22 '10 at 06:54 AM

Smile Face 100 gravatar image

Smile Face 100
24 2 4 7

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can attach this to a game object in the scene and it will walk along the x axis between 0 and 5. If it is not between 0 and 5 to start with it will walk towards (0,5) and stay there. The reason that Time.deltaTime is used is because each time slice within Update() will be different and you only want to move the object as much time that is passing for that frame (actually the game engine typically uses the time for the previous frame or some other method such as an average, but the details for this aren't important). Also, hitting the walls isn't exact since there will be some overshoot, you can fix that by chopping off the amount that you would go past the wall. I didn't want to complicate the code.

An alternate way is to use Coroutines (rather than Update), but that can be answered elsewhere.

using UnityEngine;
using System.Collections;

public class Walker : MonoBehaviour {

    public float walkSpeed = 2.0f;
    public float wallLeft = 0.0f;
    public float wallRight = 5.0f;

    float walkingDirection = 1.0f;
    Vector3 walkAmount;

    // Update is called once per frame
    void Update () {

        walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;

        if (walkingDirection > 0.0f && transform.position.x >= wallRight)
            walkingDirection = -1.0f;
        else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
            walkingDirection = 1.0f;

        transform.Translate(walkAmount);
    }
}
more ▼

answered Jun 22 '10 at 08:48 AM

jamin1001 gravatar image

jamin1001
56 5

I got a problem with the script name and he told me to rename it to something else . Sorry but i am a Beginner

Jun 23 '10 at 12:55 AM Smile Face 100

Right click in Project panel. Create -> C Sharp Script. Put your code in there. Rename the script from "NewBehaviorScript" to "Walker". The name of the script and the class name have to match. Drag & Drop onto an object in your scene, and press Play. :)

Jun 23 '10 at 01:49 AM jamin1001
(comments are locked)
10|3000 characters needed characters left

On http://Unity3d.com/support/resources/tutorials there is a brilliant tutorial entitled 3rd person platformer game. There are some AI examples and script in that tutorial. Hope this helps!!!

more ▼

answered Jun 22 '10 at 03:03 PM

Pirate___man gravatar image

Pirate___man
323 6 7 23

(comments are locked)
10|3000 characters needed characters left

Here is a waypoint based javascript that i learned from steamisM50's youtube channel

(worth checking out for tutorials youtube/streamis50)

now the actual code:

var waypoint : Transform[];

var speed : float = 20;

private var currentWaypoint : int;

var loop : boolean = false;

var Distance : float = 1;

function Awake(){

waypoint[0] = transform; //first waypoint is his starting position

} function Update () {

if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;

var velocity = rigidbody.velocity;
if(moveDirection.magnitude < Distance)
    {
        currentWaypoint++;
    }

else{
    velocity = moveDirection.normalized * speed;
}
}

else{
    if(loop)
        {
            currentWaypoint = 0;
        }
        else 
        {
            velocity = Vector3.zero;
        }
}
rigidbody.velocity = velocity;

}

more ▼

answered Jun 30 '10 at 07:52 PM

TaigaStudios gravatar image

TaigaStudios
242 3 4 16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x1420
x1070
x981
x670

asked: Jun 22 '10 at 06:54 AM

Seen: 5994 times

Last Updated: Aug 27 '12 at 04:18 AM