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 Conect11 · Nov 13, 2013 at 05:43 AM · aigravity

[Solved] NPC kinda sorta slides under player

Ok, I'm realizing that my issue might actually encompass many things here. Have been working all night to try and figure it out without having to ask the community, and have made good progress, but seem to be stuck, and am humbly requesting help. Am working with a gravity script. (below, along with the AI script, which for cleanliness of this post will be in the comments) My first problem was the NPC was falling through the terrain. (hold on, put down the angry fingers that want to type "Google it!" I did, that issue is mostly solved.) I've come to an interesting issue now that that's fixed(ish.) Whenever the npc (which is an attacking enemy, btw) gets close to the player, it stops standing upright. It almost appears to go into a slow "baseball slide", getting a leg stuck partially in the terrain. If the player walks away, the NPC rights itself, and begins pursuing the player again. Any help is genuinely appreciated. Thanks, and God bless.

alt textalt textalt text

   var gravity = 1.0;
     var verticalSpeed= 1.0;
     private var collisionFlags : CollisionFlags;  // The last collision flags returned from controller.Move
      
      
     function Update() {
        
         ApplyGravity ();
      
         // Calculate actual motion
         var movement = Vector3 (0, verticalSpeed, 0) + Vector3.zero;
         movement *= Time.deltaTime;
        
         // Move the controller
         var controller : CharacterController = GetComponent(CharacterController);
         collisionFlags = controller.Move(movement);
     }
      
      
     function ApplyGravity ()
     {
             if (IsGrounded ())
                 verticalSpeed = -0.1;
             else
                 verticalSpeed -= gravity * Time.deltaTime;
     }
      
      
     function IsGrounded () {
         return (collisionFlags & CollisionFlags.CollidedBelow) != 1;
     }


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 Conect11 · Nov 13, 2013 at 05:44 AM 0
Share

First AI Script:

 var myTransform : Transform;
 var $$anonymous$$Distance = 5;
 var follow : boolean = true;
 var damage = 8;
 var stopDistance = 4;
 
 
 
      
 function Awake(){
 myTransform = transform;
 }
      
               function Start(){
               target = GameObject.FindWithTag("Player").transform;
               }
 
      
                    function Update () {
                    if(Vector3.Distance(myTransform.position, target.position) > stopDistance){
                    animation.Play("BDGuardWalk"); 
                    }
                    else{
                    if(Vector3.Distance(myTransform.position, target.position) <= stopDistance)
                    {
                    animation.Play("BDGuardAttack");
                    Halt();
                    }
                    }
                    }
        
 function OnTriggerEnter (col : Collider) {
 if(col.gameObject.tag == "Player") { 
 Playerhealth.curHealth -= damage;
 }
 }
 
        function Halt(){
        if(Vector3.Distance(myTransform.position, target.position) < stopDistance)
        moveSpeed = 0;
        
        else{
        moveSpeed = 1;
        
        }
        }


Second AI Script

 var target : Transform;
 var moveSpeed = 1;
 var rotationSpeed = 2;
 var myTransform : Transform;
 var $$anonymous$$Distance = 30;
 var stopDistance = 4;
      
 function Awake()
 {
 myTransform = transform;
 }
      
 function Start()
 {
      
 target = GameObject.FindWithTag("Player").transform;
      
 }
      
 function Update () {
      
      
      if(Vector3.Distance(myTransform.position, target.position) < $$anonymous$$Distance)
 {
      
      
        
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        Halt();
        }
        }
        
        function Halt() 
        {
      if(Vector3.Distance(myTransform.position, target.position) < stopDistance)
       moveSpeed = 0;
        
             else{
                 moveSpeed = 1;
        
        }
        
        
        
        
        
        
        
 }

1 Reply

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

Answer by TargonStudios · Nov 13, 2013 at 12:27 PM

Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime

Ok so that makes the enemy look at the player. Problem is, when the enemy if far away from the player, it is looking at the center point of the player (it is still doing the problem, but at a much lesser scale), But when the enemy is close if the players center point is higher then the enemy, it will have to change its rotation to look up at the center point. It is easier to show you the problem through an image.

alt text

How you can fix this is either make the center point of the enemy the same height as the player, or make it so the enemy rotates, when looking at the player, only in the desired rotation. Hope this helped!


example.png (10.0 kB)
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 Conect11 · Nov 13, 2013 at 02:50 PM 0
Share

Zargo, that made perfect sense. Thank you for explaining it in a very easy to understand manner.

Solution 1 seems the simplest. $$anonymous$$erely scale the enemy. Of course, I had JUST scaled the enemy down, since he was ridiculously big before. Sigh.

Solution 2 seems best. Now have to learn how to do that, but this is a GREAT start. Thank you so much!

EDIT: Hmmm, I wonder if placing a gameobject lower on my player, parenting it, and setting THAT as the reference point might help...

EDIT 2:

Fascinating. It DOES help. However, if I set the transform to that reference point in the inspector, and then gametest, it automatically switches back to the parent object. (in this case, the player) I have to manually switch BAC$$anonymous$$ to the reference object during gametest, which of course A: isn't going to happen when someone is playing the game, and B: Can't be saved.

Edit 3, and solution:

Ok Zargo, you rock! The trick to not letting the script change to the parent object of my player reference point was simply to change which tag it was searching for. Since the script was searching for tag: "Player," it naturally switched to that. (and away from the Player Reference Point) Once I changed the tag searched for in the script to one matching the reference point, the entire issue resolved itself straight away. Thanking God! =)

avatar image TargonStudios · Nov 13, 2013 at 08:21 PM 0
Share

Very glad I could help you out. I remember when I had this very same problem, hated it lol. Hope your game goes well.

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

17 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

Related Questions

Enemy Rigidbody Not Responding to Gravity 1 Answer

Best way to keep an ai grounded? 1 Answer

Object floating and going through walls/ground. 1 Answer

No gravity on NPC. Floating above the ground. 2 Answers

Applying Gravity to AI on chase 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