- Home /
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:
Here is the image of components in NPC and Player:
NPC:
Player:
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:
Here is the .Unity3D file:
Here is the .zip file (contains html and .unity3d file):
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.
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.
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
Follow this Question
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