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
0
Question by lehermj · Jan 03, 2015 at 11:49 AM · collideraienemy

How can I make my enemy stay on the ground and collide with other objects?

In my script the enemy chases the player until it is close enough to attack, but they enemy doesn't stay on the ground and flies through walls. How can I make it obey gravity? here's my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EnemyChase : MonoBehaviour {
 
     public Transform Player;
     public int MoveSpeed = 4;
     public int MinDist = 5;
     public int MaxDist = 10;
     public Slider healthBarSlider;
 
     bool attack;
 
     void Update() {
         //squares up
         transform.LookAt(Player);
         //chases player
         if (Vector3.Distance (transform.position, Player.position) <= MaxDist) {
             transform.position += transform.forward * MoveSpeed * Time.deltaTime;
             //stops doing damage
             attack = false;
             Debug.Log ("get over here and fight like a man!");
             //stops and attacks when close enough to player
             if(Vector3.Distance(transform.position,Player.position) <= MinDist) {
                 Debug.Log ("PEWEWPEWEPWEWPEPWP");
                 //starts doing damage
                 attack = true;
             }
         }
         //applies damage to player
         if (attack) {
             healthBarSlider.value -= 0.005f;
         } else if (healthBarSlider.value == 0) {
             //die
         } else {
             healthBarSlider.value += 0.0f;        
         }
     }
 }
 

Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alemimi · Jan 03, 2015 at 02:49 PM

Did you add Collider components to every wall, ground and enemy? And in order for the enemy to "stop" when colliding with something, i think it needs to have a "Rigidbody" component attached to it, try and see if that works.

Comment
Add comment · Show 2 · 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 lehermj · Jan 04, 2015 at 04:08 AM 0
Share

should i check if its grounded, and if not add force or somethin?

avatar image SandipVala · Apr 08, 2016 at 06:48 AM 0
Share

For checking colliding enemy or not use tag or gameobject name for apply force or changes velocity !!! Rigidbody needed.

avatar image
1

Answer by Jabeebus · Apr 08, 2016 at 06:38 AM

One way to make it stay is to add a Rigidbody to it and enable gravity

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy gets through wall over time, colliders working. 1 Answer

enemy awareness with colliders? 1 Answer

Enemy Color Change Help! 1 Answer

Best way to handle melle attacks in a fast paced 2D game 0 Answers

Send an enemy back to its spawn point using waypoints 2 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