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)
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.
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
Your answer
Follow this Question
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
Using AddForce for horizontal movement doesn't quite work the way I want it to (2D) 1 Answer
Problem with my dash ability, 1 Answer