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 /
  • Help Room /
avatar image
0
Question by blakemontaque33 · Apr 11, 2019 at 03:09 PM · collisionontriggerenter

Cyclist detecting player

I'm working on a game and I have a my player with the tag "Player". I have a cyclist spawning system, and each cyclist has the tag "cyclist", they are all prefabs. I have a script on the cyclists prefab that makes it move forwards, so each time it spawns, it instantly begins to move in a fixed direction.

I want the cyclists to be able to detect 2 things, one being if the player is in front of it, and the other if another cyclist is in-front of it. If so, I want the cyclist to stop cyclist. I have a script called cyclistStoping.cs that I'm using to do this. The script has been placed onto the cyclist prefab for multiple instances to be spawned.

Bug

I've noticed that sometimes even once a cyclist(a) is no longer in front of another cyclist(b), cyclist(b) will still remain still, and it will only move off again if the player walks in front of it and then walks off again. I feel like my code is just buggy and would really appreciate some help on this.

I've tried to do 2 simple checks inside each trigger function but some cyclists still remain idle when once the cyclist is no longer in front of them.

I have also tried using a delay function so whenever the cyclist or player are no longer in range, the cyclist doesn't move off until 2-3 seconds have passed. However with this, the cyclist doesn't move off again.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
 
  public class cyclistStoping : MonoBehaviour
  {
 
  public VehicleMove cyclistMovement;
 
 
  public bool playerInside = false;
  public bool cyclistInside = false;
 
  private bool timePassed = false;
 
  void Start()
  {
 
  }
 
  void Update()
  {
 
  }
 
  // implement delay
 
  // player or cyclist (INSIDE)
  private void OnTriggerEnter(Collider other)
  {
  if (other.gameObject.tag == "Player" ) {
      playerInside = true;
      cyclistInside = false;
  }
 
  else if (other.gameObject.tag == "Cyclist")
  {
      playerInside = false;
      cyclistInside = true;
  }
 
  if (playerInside == true || cyclistInside == true) {
      Debug.Log("Player inside: " + playerInside);
      cyclistMovement.vehicleMovement = 0.0f;
 
  }
  }
 
  // player or cyclist (EXIT)
  private void OnTriggerExit(Collider other)
  {
  if (other.gameObject.tag == "Player")
  {
      playerInside = false;
      cyclistInside = false;
  }
 
  else if (other.gameObject.tag == "Cyclist")
  {
      playerInside = false;
      cyclistInside = false;
  }
 
  if (playerInside == false || cyclistInside == false)
  {
 
      if (timePassed == true) {
          Debug.Log("Player inside: " + playerInside);
          cyclistMovement.vehicleMovement = 0.1f;
          // delay, then move off
          timePassed = false;
      }
  }
  }
 
  IEnumerator timeDelay()
  {
      // wait before moving off
      yield return new WaitForSeconds(3);
      timePassed = true;
      }
 
      }

I expect the cyclist to stop if a player is in front of it, and once the next cyclist spawn, I expect THAT cyclist to stop once it detects the first cyclist. Once the player moves away from the first cyclist, it should take 2 seconds before moving off, and the second cyclist will do exactly the same.

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 Vladislav-Videnov · Apr 11, 2019 at 04:02 PM

 if (playerInside == false || cyclistInside == false)
   {
  
       if (timePassed == true) {
           Debug.Log("Player inside: " + playerInside);
           cyclistMovement.vehicleMovement = 0.1f;
           // delay, then move off
           timePassed = false;
       }


It feels weird that if you want the cyclist to start moving again when there is no other cyclist and player .. why you have OR in your if statement ? It should be && instead of ||.

Also .. when you are setting the booleans in OnTriggerEnter

   if (other.gameObject.tag == "Player" ) {
       playerInside = true;
       cyclistInside = false;
   }

You cant assume that if the player entered there is no cyclist inside .. what if there was a cyclist already inside ? Same goes for the next if statement.

Also i do not see you using your timeDelay function ...

I would change it to

 if (playerInside == false && cyclistInside == false)
   {
      StartCoroutine(timeDelay());
   }

and then the timeDelay() to :

  IEnumerator timeDelay()
   {
       yield return new WaitForSeconds(yourwaittime);
       //reset speed of cyclist.
   }


Thats all i could help with, as i am currently still at work. Can help you more later. Good luck !

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

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

Lose Health when GameObject enters collider? (OnTriggerEnter) 2 Answers

Why is OnTriggerEnter not working properly? 2 Answers

Function OnTriggerEnter2D is not called 1 Answer

How to deadtivate a gameobjects from the scene, if player has a character controller 0 Answers

Make object(s) stack vertically. 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