Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zitoox · Sep 06, 2016 at 10:52 PM · scripting problemphysicsbeginnersphereenemy ai

Make enemy follow and hit the player

Hello everyone, sorry for making SO MANY questions in one day, i am curious and i want to learn xD

I am making a scene where the player is a sphere, and he has an enemy sphere that need to "kill" him. I have a script that i've found that allow the sphere to follow the player, but it's literally impossible.

The enemy sphere COMPLETELY ignores any obstacle, can fly, it doesn't seems to care about physics, it's just impossible to run away from it. I am using a sphere controller based on the Unity's rollerball.

So, do anyone know what do i need to change in the script so it "respects" the standard physics, and move more like a player, and not just fly around in a way that it appear that it is rolling?

Here's the script, it's JavaScript:

  var Player : Transform;
  var MoveSpeed = 4;
  var MaxDist = 10;
  var MinDist = 5;
  
  
  
  
  function Start () 
  {
  
  }
  
  function Update () 
  {
      transform.LookAt(Player);
      
      if(Vector3.Distance(transform.position,Player.position) >= MinDist){
      
           transform.position += transform.forward*MoveSpeed*Time.deltaTime;
  
            
            
           if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
               {
                  //Here Call any function U want Like Shoot at here or something
     } 
     
     }
  }

I would be grateful if anyone helped me. I am a beginner, and unfortunely, i don't know anyone that could teach me anything, and there are no courses,universities,etc... in my city. The tutorials in youtube and the documentation help, but not much =(

Comment
Add comment · Show 1
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 doublemax · Sep 07, 2016 at 06:43 AM 0
Share

I would suggets to use a Nav$$anonymous$$eshAgent for that. You should check the "Survival Shooter tutorial" from Unity. It shows exactly what you need:

https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial

Part 4 is about the enemy behavior: https://unity3d.com/learn/tutorials/projects/survival-shooter/enemy-one?playlist=17144

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ProgramacionWH · Sep 07, 2016 at 12:29 AM

Changing position " transform.position += transform.forward*MoveSpeed*Time.deltaTime;" ignores physics it is like a teleport .

So you have to use a Character Controller or a Rigidbody Components. In the case of a rigidbody you can add force to it:

 gameObject.GetComponent.<Rigidbody>().AddForce(transform.forward ... etc);

If you use rigidbody, remember to change its Drag value (How much air "stop" the movement of the object) to a higher value in order to make it inertia smaller.

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 Zitoox · Sep 07, 2016 at 02:05 AM 0
Share

What do you recommend to use in the addforce method? I didn't find any source of this command in Javascript, only in C#

avatar image
0

Answer by millej23 · Sep 07, 2016 at 12:29 AM

Hello!

Have you added a rigidbody and collider to the enemy object?

Also, if you are wanting the enemy to avoid obstacles, etc, you may need to setup a NavMesh agent. See here:

https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

Let me know if that helps at all, thanks!

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 Zitoox · Sep 07, 2016 at 02:02 AM 0
Share

Yes, i am using rigidbody. I think that if i use navmesh it would be strange. As far as i know, navmesh is predictated, right? I needed something that followed the player. Anyway, thanks =)

avatar image
0

Answer by unityBerserker · Sep 07, 2016 at 12:02 AM

@Zitoox

  1. If you want to use physics on object you should add Rigidbody and Collider components. Rigidbody simulate physics( like reaction to forces), colliders - react with other object (like blocking them).

  2. Rigidbody(NvidiaX -physic engine) and Rigidbody2D(Box2D) don't interract with themselves. These are using two different physic engines. You can only interact components with same ending of component name (2D or none). Same rules are for colliders.

  3. If you move object it need Rigidbody component. In that component is isKinematic property. If this option is unchecked object react with other object ( to forces) - to move that object you should use .addForce method.

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

If isKinematic is set to true - object not react with forces. You should move that gameobject it with transform.
alt text
4. Colliders have ability to colliding with other gameobject (both must have colliders). Colliders has property isTrigger. If we mark is as true - this object will not react with others colliders. alt text 5. If you moving object using physics you should do it in FixedUpdate() not Update() - movement will be more fluent.

Those are most important thing for begging (but not all).


screenshot-5.png (8.3 kB)
screenshot-6.png (8.1 kB)
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 Zitoox · Sep 07, 2016 at 02:13 AM 0
Share

Thank you for the hints, the FixedUpdate ins$$anonymous$$d of just Update helped a lot, the movements are much more smooth now, but the enemy is still ignoring the physics, as it doesn't even move when i hit it.

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

98 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 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

Ball bounces unexpectedly while rolling downwards 0 Answers

Calculating Sphere vs. Plane Friction 1 Answer

Sphere Friction Not Working Correctly When Bouncing Hard 0 Answers

How can I slow the velocity of my player without it affecting fall speed? 0 Answers

How to set a limit for my player rotation ? 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