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 Yunnan · Feb 22, 2015 at 04:23 PM · c#npcpatrol

Prevent bumping of game object into other game object

I have made simple Patrol NPC in Unity and everything works fine and NPC has it is own speed (NPC could move to the end point and stop for a while, and goes back to start and stop for a while, this will continuously happen), and I also make the player could move horizontal (a, d in the keyboard) in x axis and vertical (w, s in the keyboard) in z axis and the player has it is own speed. However, when I run the game and starts to move closer to the NPC, the player and NPC will bump into each other, resulting NPC position changes.

Here is the video of it:

Video Link

Here is the image of components in NPC and Player:

NPC:

NPC Components

Player:

Player Components

I have tried to change the constraints in the RigidBody of the NPC components by freeze all position and rotation and also I have tried to check the isKinematic, the problem solved. But when the Player stand in front of the NPC, the NPC just go through the Player, like the collider of the Player is triggered.

Here is the code that I am using:

NPC Script:

 public class NPCManager : MonoBehaviour 
 {
     private PlayerManager _playerManager;
 
     private bool moveForward = false;
 
     private float movementSpeed = 1.5f;
 
     private void Awake()
     {
         _playerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
     }
 
     private void Update()
     {
         if (moveForward)
         {
             transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
         }
 
         else
         {
             StartCoroutine(Hold());
         }
     }
 
     private void OnGUI()
     {
         if (_playerManager.isOnChat)
         {
             GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 250, 100), "Test.");
         }
     }
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.name == "First Point")
         {
             moveForward = true;
         }
 
         else if (col.gameObject.name == "Second Point")
         {
             moveForward = false;
         }
     }
 
     private IEnumerator Hold()
     {
         yield return new WaitForSeconds(2.0f);
 
         transform.Translate(-Vector3.forward * movementSpeed * Time.deltaTime);
     }
 }

Player Script:

 [RequireComponent(typeof(CharacterController))]
 public class PlayerManager : MonoBehaviour 
 {
     private CharacterController controller;
 
     private Vector3 moveDirection = Vector3.zero;
 
     private float gravity = 20.0f, speed = 5.0f;
 
     public bool isOnChat = false;
 
     private void Awake()
     {
         controller = GetComponent<CharacterController>();
     }
 
     private void Update()
     {
         if (controller.isGrounded)
         {
             gameObject.renderer.enabled = true;
 
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 
             moveDirection = transform.TransformDirection(moveDirection);
 
             moveDirection *= speed;
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
 
         controller.Move(moveDirection * Time.deltaTime);
     }
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.name == "NPC Follower")
         {
             isOnChat = true;
         }
     }
 
     private void OnTriggerExit(Collider col)
     {
         if (col.gameObject.name == "NPC Follower")
         {
             isOnChat = false;
         }
     }
 }

How can I solve this?

Any help will be much appreciated!

Thank you so much!

Here is the Web Player file:

Web Player

Here is the .Unity3D file:

.Unity3D

Here is the .zip file (contains html and .unity3d file):

ZIP File

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Domo23000 · Feb 23, 2015 at 03:15 AM

On the NPC, go to their rigidbody, and check all of their constraints. That should work.

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 Yunnan · Feb 23, 2015 at 08:43 AM 0
Share

I have tried to change the constraints in the RigidBody of the NPC components by freeze all position and rotation and also I have tried to check the is$$anonymous$$inematic, the problem solved. But when the Player stand in front of the NPC, the NPC just go through the Player, like the collider of the Player is triggered.

I don't want like this sir, I just want the NPC will stop whenever there is a player in front of the NPC and not bump also.

avatar image Domo23000 · Feb 25, 2015 at 12:12 AM 0
Share

Okay then forget what I said earlier.

For that the best way to do it is raycasting and a boolean.

in the npc script ask if the boolean can$$anonymous$$ove is true or whatever name you want, and then cast two rays in either direction. on the player set its layer to player, and ask if the raycast has hit an object with the layer called player. you could also just ask if the hit object has a tag called player and give the player a player tag.

 public bool can$$anonymous$$ove;
 
 void Update()
 {
     if(can$$anonymous$$ove)
     {
         $$anonymous$$ove();
     }
 
     RaycastHit hit;
 
     if(Physics.Raycast("RaycastStuff"))
     {
         if(hit.collider.gameObject.tag == "Player")
         {
             can$$anonymous$$ove = true;
         }
         else
         {
             can$$anonymous$$ove = false;
         }
     }
 }

this is not tested but if you add the raycast with vector3.forward or something, then copy and paste it with -vector3.forward and this should work. if it doesn't im pretty sure it can be fixed easily, but if you need anything just ask me.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NPC won't move after adding chase code. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Mecanim Making an NPC walk 1 Answer

Need Help Scripting Weapons Using C# 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