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 JohnColvin · Apr 18, 2017 at 09:53 AM · 2draycastlinecast

2D linecast is allowing NPCs to move onto same position

After finishing the 2d roguelike tutorial, I started to play around with the code. Specifically, changing it to be realtime instead of turn based. I've run into an issue where NPCs occasionally move onto the same position. I've tried a few fixes, but the problem has remained. Any help in understanding what I'm doing wrong or what the underlying issue is would be a great help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Character : MonoBehaviour {
 
   private BoxCollider2D boxCollider;
   private float inverseMoveTime;
   private float moveTime = .04f;
   bool movingEnabled = true;
   private Rigidbody2D rigidBody;
 
   protected void attemptMove(int x, int y) {
     if (!movingEnabled || (x == 0 && y == 0)) return;
 
     Vector3 movement = new Vector3(x, y, 0);
     Vector3 endPos = transform.position + movement;
 
     RaycastHit2D hit = checkForHit(endPos);
 
     if (hit.transform == null) {
       move(endPos);
     }
   }
 
   RaycastHit2D checkForHit(Vector3 endPos) {
     boxCollider.enabled = false;
     RaycastHit2D hit = Physics2D.Linecast(transform.position, endPos);
     boxCollider.enabled = true;
     return hit;
   }
 
   void move(Vector3 endPos) {
     StartCoroutine(moveSmoothly(endPos));
     // StartCoroutine(moveChoppy(endPos));
   }
 
   IEnumerator moveChoppy(Vector3 endPos) {
     movingEnabled = false;
     rigidBody.MovePosition(endPos);
     yield return new WaitForSeconds(moveTime);
     movingEnabled = true;
   }
 
   protected IEnumerator moveSmoothly(Vector3 endPos) {
     movingEnabled = false;
     float sqrRemainingDistance = (transform.position - endPos).sqrMagnitude;
     while(sqrRemainingDistance > float.Epsilon) {
       Vector3 newPostion = Vector3.MoveTowards(rigidBody.position, endPos, inverseMoveTime * Time.deltaTime);
       rigidBody.MovePosition(newPostion);
       sqrRemainingDistance = (transform.position - endPos).sqrMagnitude;
       yield return null;
     }
     movingEnabled = true;
   }
 
   protected virtual void Start () {
     boxCollider = GetComponent<BoxCollider2D>();
     inverseMoveTime = 1f / moveTime;
     rigidBody = GetComponent<Rigidbody2D>();
   }
 
   void Update () {
     attemptMove(Random.Range(-1, 2), Random.Range(-1, 2));
   }
 }
 

The things I've tried to fix it:

  • Use a game manager to iterate over the NPCs calling a custom update method. This was to force an order, thinking it might be a concurrency issue.

  • Move directly to the position instead of using a coroutine to smoothly move. Thinking that the second NPC could still think the position is unoccupied since the first one has only begun to move into it.

  • Use Collider2D.Raycast instead of temporarily disabling the collider to use Physics2D.Linecast. Thinking the second NPC is doing its calculation while the first one has its collider disabled.

I'm at a loss about what else to try. Any links to guides that explain how these things are interacting would be great, too. Thanks!

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

0 Replies

· Add your reply
  • Sort: 

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

160 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

Related Questions

how to stop mouse from teleporting or detect better, so that i can detect if a swipe of mouse was over object 0 Answers

How do I Implement Linecast2d 0 Answers

Checking for Raycast distances not working as expected. 0 Answers

Feild of view only wokring in specific areas of the scene 0 Answers

Inconsistent raycast2D reflecting 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