Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Smile Face 100 · Jun 22, 2010 at 06:54 AM · 2dmovementaienemy

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)).

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Onix · Apr 07, 2014 at 06:51 AM 0
Share

this is work wonder omg thanks

avatar image flamy · Apr 07, 2014 at 06:52 AM 0
Share

if any of the answers helped you, Do accept the correct answer

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best 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);
 }

}

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smile Face 100 · Jun 23, 2010 at 12:55 AM 0
Share

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

avatar image jamin1001 · Jun 23, 2010 at 01:49 AM 1
Share

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. :)

avatar image Codfish1114 · Mar 15, 2016 at 01:24 PM 0
Share

I'm getting a lot of errors when I enter this, is this in C# or Javascript?

avatar image godpers · Apr 30, 2020 at 10:53 AM 0
Share

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);
           }
 
  }
  
 }


avatar image
1

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!!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges