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 Vomdrache · Mar 12, 2016 at 05:49 PM · c#collisionmovementraycasthit2d

Issue with character movement and collision using Physics2D.Raycast.

Hey guys, I've gotten this bug multiple times in the past trying to create a snap-movement game.

In the shot bellow you can see that the character is at (-7, 0) and attempting to move to (-6, 0).

This is my current script to handle movement:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     
     Vector3 pos;
     public float speed = 1.5f;
     private int collisionLayers = 7 << 8;
 
     void Start () {
         pos = transform.position;
     }
 
     void FixedUpdate () {
         if(Input.GetKey(KeyCode.A) && transform.position == pos) {
             CheckCollision(pos + Vector3.left);
         }
         if(Input.GetKey(KeyCode.D) && transform.position == pos) {
             CheckCollision(pos + Vector3.right);
         }
         if(Input.GetKey(KeyCode.W) && transform.position == pos) {
             CheckCollision(pos + Vector3.up);
         }
         if(Input.GetKey(KeyCode.S) && transform.position == pos) {
             CheckCollision(pos + Vector3.down);
         }
 
         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
     }
 
     void CheckCollision(Vector3 destination){
         Debug.Log (transform.position);
         Debug.Log (destination);
         RaycastHit2D hit = Physics2D.Raycast (transform.position, destination, 1f, collisionLayers);
         if (hit.collider == null) {
             pos = destination;
         } else {
             Debug.Log (hit.collider.gameObject + ": (" + hit.collider.transform.position.x + ", " + hit.collider.transform.position.y + ")");
         }
     }
 };

Expected response:

  • Using Raycasting2D a line is drawn between one point and another, if the line comes into contact with any colliders it can be saved as a 'hit'.

  • When drawing a line from the character's position (-7, 0) to (-6,0) there should be no collision taking place.

  • The character may then continue movement to (-6, 0) "one tile to the right"

alt text

What is happening:

  • When attempting to move into non-border tiles movement works perfectly

  • Sometimes the character will not be able to move to a non-border tile if the character is next to a border tile.

What I think may be wrong:

In my code I have Physics2D.Raycast (transform.position, destination, 1f, collisionLayers); due to my very limited knowledge on how this Raycast works I can assume that I'm using the 3rd argument for distance incorrectly.

Each tile is one world unit so I assumed that the distance I would like to measure is 1f.

I've been working all day on this (and a few other days on a similar project with the same issue) with no luck to finding something similar online. This is my first time posting only for code questions so I hope I haven't left out any important details.

screen-shot-2016-03-10-at-60103-pm.png (151.7 kB)
Comment
Add comment · Show 1
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 Salmjak · Mar 12, 2016 at 10:13 PM 0
Share

@Vomdrache The problem is probably this: destinationshould be the direction not the goal.

The direction would be only Vector3.right not pos + Vector3.right! Since you actually use the destination to set the new goal you should pass another Vector3 direction as a parameter.

It would look something like this:

 CheckCollision(pos + Vector3.left, Vector3.left);
 void CheckCollision(Vector3 destination, Vector3 direction){
          Debug.Log (transform.position);
          Debug.Log (destination);
          RaycastHit2D hit = Physics2D.Raycast (transform.position, direction, 1f, collisionLayers);
          if (hit.collider == null) {
              pos = destination;
          } else {
              Debug.Log (hit.collider.gameObject + ": (" + hit.collider.transform.position.x + ", " + hit.collider.transform.position.y + ")");
          }
      }





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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to SHOOT an object on a curved path without using Rigidbody 2 Answers

How can I remove movement or camera rotation in one direction with a trigger 0 Answers

OnMouseDown Not Working after Moving 0 Answers

Changing moving direction after hit other object 0 Answers

Move an Object until it collides with wall? 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