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 NicolasK98 · Oct 21, 2019 at 06:09 PM · raycasthit2dboxcollider2dgrid based gamehit detection

Issue with RaycastHit2D and object collider

Hello, I'm pretty new with raycasting in Unity.
I was working on a grid base movement for a 2D game and I tryed to write a test code to figure out how to predict if in the next "cell" of the grid there would be a block with a BoxCollider2D attached (like a wall).
The code I wrote works fine but when the Player moves sometimes return a raycast collision that doesn't exist with objects placed at different global coordinates.
I can't find any fixes to this problem and I don't know where I'm wrong, if someone can help me I'll be very grateful, thank you.

The script needs to be attached to a empty GameObject with only a RigidBody2D set to "Dynamic", "Gravity Scale = 0" and Freeze Rotation enabled.
The Player moves by 1U per axis Input and the box colliders, of the Objects (like the walls), needs to be size of 1 by 1.
I'm using Unity 3D 2019.2.9f1


"Player" script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Player : MonoBehaviour
 {
     public Vector2 nextPosition;
     public Vector2 targetPosition;
 
     public float movementSpeed = 10.0f;
 
     private Rigidbody2D rb2D;
     private BoxCollider2D bc2D;
     private bool isAxisInUse; 
 
     void Start()
     {
         rb2D = GetComponent<Rigidbody2D>();
         //bc2D = GetComponent<BoxCollider2D>();
         isAxisInUse = false;
 
         targetPosition = new Vector2(rb2D.position.x, rb2D.position.y);
         nextPosition = new Vector2(rb2D.position.x, rb2D.position.y);
     }
 
     void FixedUpdate()
     { 
         if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
         {
             if (Input.GetAxisRaw("Horizontal") > 0 && isAxisInUse == false)
             {
                 isAxisInUse = true;
                 nextPosition = nextPosition + new Vector2(1, 0); //set the next position where the Player wants to move
             }
             else if (Input.GetAxisRaw("Horizontal") < 0 && isAxisInUse == false)
             {
                 isAxisInUse = true;
                 nextPosition = nextPosition - new Vector2(1, 0);
             }
 
             if (Input.GetAxisRaw("Vertical") > 0 && isAxisInUse == false)
             {
                 isAxisInUse = true;
                 nextPosition = nextPosition + new Vector2(0, 1);
             }
             else if (Input.GetAxisRaw("Vertical") < 0 && isAxisInUse == false)
             {
                 isAxisInUse = true;
                 nextPosition = nextPosition - new Vector2(0, 1);
             }
         }
         else isAxisInUse = false;
 
         RaycastHit2D hit = Physics2D.Raycast(nextPosition, nextPosition); //spawn raycast inside the "nextPosition" grid.
         if (hit.collider != null) //if a raycast hits a collider
         {
             Debug.Log("Collider at " + hit.point + " " + hit.collider.name);
             nextPosition = targetPosition; //set "nextPosition" as the current position of the Player -> the player won't move
         }
         else
         {
             targetPosition = nextPosition; //update the target position for the Vector2.MoveTowards
             rb2D.position = Vector2.MoveTowards(transform.position, targetPosition, Time.deltaTime * movementSpeed); //move the rb2d towards the target position
         }
     }
 }

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

185 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

Related Questions

Weird hitcollider problems 1 Answer

Problems using RaycastHit2D.collider, detection only on the rays origin 1 Answer

two boxCasts casted from same point give different results 0 Answers

Geting error: CS1525 Unexpected symbol `private' lines (34/15) 1 Answer

Trying to make a grid with rectangular nodes 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