- Home /
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)).
if any of the answers helped you, Do accept the correct answer
Answer by jamin1001 · Jun 22, 2010 at 08:48 AM
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);
}
}
I got a problem with the script name and he told me to rename it to something else . Sorry but i am a Beginner
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. :)
I'm getting a lot of errors when I enter this, is this in C# or Javascript?
This is the correct code, the response one has some format issues.
public class Walker : $$anonymous$$onoBehaviour {
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);
}
}
}
Answer by Pirate___man · Jun 22, 2010 at 03:03 PM
On 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!!!
Answer by TaigaStudios · Jun 30, 2010 at 07:52 PM
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;
}
Answer by AmarokStudios · Jul 22, 2014 at 01:29 PM
You can apply the following script to the object you'd like moving side to side. Make sure you leave "Vert" unchecked. If you check it, then the object will move up and down instead of side to side. (Useful for other purposes.) You can change the "Max step" var from the inspector to change how far the object moves. Changing the "Step" variable will change how fast the object moves. All you'd need to do is attack an attack function and you're good to go!
pragma strict
private var Xpos : float; private var Ypos : float; private var max : boolean;
var Vert : boolean; var maxAmount : int; var step : float;
function Start () { Xpos = transform.position.x; Ypos = transform.position.y; }
function FixedUpdate () {
if (Vert) { if (transform.position.y >= Ypos + maxAmount) { max = true; }
else if (transform.position.y <= Ypos)
{
max = false;
}
}
else
{
if (transform.position.x >= Xpos + maxAmount)
{
max = true;
}
else if (transform.position.x <= Xpos)
{
max = false;
}
}
if (Vert)
{
if (!max)
{
transform.position.y += step;
}
else
{
transform.position.y -= step;
}
}
else
{
if (!max)
{
transform.position.x += step;
}
else
{
transform.position.x -= step;
}
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
enemy AI patrol not working 1 Answer
Enemy AI With changing Player 0 Answers
No movement when animation plays (2d) 1 Answer
2D Random AI 1 Answer