Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I un-invert my first person spaceship controls in C#? 1 Answer

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

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

Enemy AI Movement Decision Making 1 Answer

NavMeshAgent, goes to the wrong place 0 Answers


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