• 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 /
  • Help Room /
avatar image
Question by Codfish1114 · Mar 14, 2016 at 01:15 PM · movement2d game2d-platformerenemyai

Unity 2D Enemy movement problems

Currently, all my script does is make them fly and follow the player (which is cool but not what I'm looking for for most of the enemies), I'm trying to have enemies actually walk on the ground like the player does, here's the script I'm using using UnityEngine; using System.Collections; public class Enemy_movement : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; void Start() { target = GameObject.Find("Michael").transform; } void Update() { if (target != null) { Vector3 dir = target.position - transform.position; // Only needed if objects don't share 'z' value. dir.z = 0.0f; if (dir != Vector3.zero) transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.FromToRotation (Vector3.right, dir), rotationSpeed * Time.deltaTime); //Move Towards Target transform.position += (target.position - transform.position).normalized * moveSpeed * Time.deltaTime; } GetComponent<Rigidbody2D>().velocity = Vector3.zero; } } any suggestions on what to change (I'm still very new to game design and am using other posts to help me put my code together)

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by Zoogyburger · Mar 14, 2016 at 05:32 PM

Since your code is not formatted, it makes it really hard to read... On your enemies' Rigidbody2d you could just increase their gravity to make them fall on the ground.

Comment

People who like this

0 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 Codfish1114 · Mar 15, 2016 at 01:06 PM 0
Share

I tried this, no luck, thanks anyway.

avatar image

Answer by ComradeVanti · Mar 15, 2016 at 02:41 PM

Codfish1114

How is your setup? Do you have a xy-plane to move your characters on? If so you could forget about the whole rotate thing and just invert the movement speed of your enemys if your players x changes in relation to them.

Example:

if (Player.tranfrom.position.x > enemy.transform.position.x) enemy.speed = 10; else enemy.speed = -10;

Or something like that. If this doesnt help you please give more details on what you want to have here :D

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

56 People are following this question.

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

Related Questions

How to cancel the force caused by collision? So the player is not pushed away when it hits a corner? 0 Answers

How to create a movement script for my person by controlling him by joystick 0 Answers

My character is shaking while walking. 2D game 0 Answers

Why is my character movement jerky/stuttry/jettry? 2 Answers

Enemy is shooting the opposite direction of player 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