• 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
0
Question by ImSLake · Jul 22, 2015 at 02:07 PM · scripting problemmovementaibeginner

NPC movement

I'm very new to programming and working in unity, and i just started to develop my first game (a 2D side scroller). So the thing i'm trying to do here is to make an enemy NPC walk back and fourth on a platform. The way i first thought of doing this was to make two unrendered boxes with box colliders working as triggers on the edges of the platform so that when the NPC would hit the boxes he would turn around and walk to the other edge and thus hit the other box and do the same thing over again. I don't know if i did it sort of right but this is the way i did it:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour {
     // Public values
     public float Speed;
     // Private values
 
 
     // Use this for initialization
     void Start () {
     
     }
 
     // Object movement
      
     
         void OnTriggerEnter2D(Collider2D other)
         {
             if(other.name == "EnemyPathBox")
             {
                 transform.position = Vector2.right * Speed * Time.deltaTime;
             }
             else if(other.name == "EnemyPathBox1")
             {
                 transform.position = Vector2.right * Speed * Time.deltaTime;
             }
         }
     
 
     // Update is called once per frame
     void Update () {
     
     }
 }

Where EnemyParhBox and EnemyParhBox1 are the boxes on the edges of the platform. I think the idea is good enough (i don't know though) but i just have one problem, whenever the NPC hits one of the boxes it goes to the position [0,0] i have no clue why this is happening. One other thing, I don't really know how to make it move in the first place so if anyone could help with that too, it would be nice.

Comment
Add comment · Show 18
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 ImSLake · Jul 22, 2015 at 01:13 PM 0
Share

Thanks for the help. Well it definitely did something different. Now it's stuck in [0,0].

avatar image Hellium · Jul 22, 2015 at 01:16 PM 1
Share

Don't forget to set the speed to a value greater (or lower) than 0.

$$anonymous$$oreover, your enemy is not in the center of the scene I guess and you have placed it in a specific place. Then, gets its initial position in the Start function, and in the Update :

 transform.position = Vector2.right * Speed * Time.deltaTime + initialPosition;

(initialPosition is a private Vector3 of your class)

In the Start function :

 initialPosition = transform.position ;
avatar image Hellium · Jul 22, 2015 at 01:37 PM 1
Share

Oops, I am not familiar with 2D games, sorry.

  • Are you sure the Speed value is different from 0 ?

  • Are you sure the enemy object holds the script ?

  • Are you sure the script hold by the enemy is enabled ?

avatar image Hellium · Jul 22, 2015 at 01:51 PM 1
Share

Do your colliders are touching each other ?

If so, they should not. They must be set as follow :

 []__________[]

The [] are colliders The __ are your platform

avatar image Hellium · Jul 22, 2015 at 01:59 PM 1
Share

That's strange. $$anonymous$$ake sure the Update is running : add a Debug.Log("Update is running");

There is another solution I guess, but I think the one we are trying to implement is the easiest.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by alexi123454 · Jul 22, 2015 at 02:29 PM

Instead of using two colliders on the edges of the platform, you could just parent a single collider to the lower corner of the player with a script on it. Set the collider to trigger, and implement the "OnTriggerExit" function in it. Inside the function, change the character's direction. This way, you can place the NPC on any platform that drops off and it should correctly stay on the platform (every time the character reaches the edge of the platform, the collider stop colliding with the ground, calls OnTriggerExit, and causes the character to turn around).

If you want to place them on a platform that has walls on either side instead of drops, you could also implement the "OnTriggerEnter" with an if statement checking if the colliding object is a wall, and cause the character to turn around as well.

Also, the reason your NPC is just being teleported to a position is because you're setting the position to the speed, rather than adding the speed to the position. Every frame you're setting the position to

Vector2.right * Speed * Time.deltaTime

which is going to be the same number every frame (Vector2.right is a constant value, speed is a constant value, and Time.deltaTime only changes a tiny tiny bit between frames). Since you're setting it to the same number each frame, it's not going to move anywhere :P To make it change position, you need to do

transform.position += value;

instead of

transform.position = value;

Comment
Add comment · Show 1 · 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 ImSLake · Jul 22, 2015 at 02:49 PM 0
Share

Ahhhh, totally forgot OnTriggerExit even exists. Thank you for the help.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to restrict first person from turning 360 degrees when sitting? 1 Answer

how to make 2D platformer AI for a skeleton 0 Answers

I cant figure out how to change my simple Movement controls to touch, can anyone help? 1 Answer

I Have made a movement script for my pacman sprite. But animations keep playing even when there is no input. ans when i drag my sprite around in the scene it keeps trying to return to its original position. 0 Answers

Enemy AI Movement Decision Making 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges