Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

unity3d - Raycast2D hits only one side of Collider

I want to make sure that various objects moving at high speed cannot pass through walls or other objects. My thought process was to check via Raycast if a collision has occurred between two moments of movement.

So the script should remember the previous position and check via Raycast for collisions between previous and current position.

If a collision has occurred, the object should be positioned at the meeting point and moved slightly in the direction of the previous position.

My problem is that works outside the map not inside. If I go from inside to outside, I can go through the walls. From outside to inside not.

Obviously I have misunderstood something regarding the application with raycasts.

MapLayout with Walls

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

public class ObsticalControll : MonoBehaviour
{

    private Vector3 positionBefore;
    public LayerMask collisionLayer = 9;
    private Vector3 lastHit = new Vector3(0, 0, -20);
    // Start is called before the first frame update
    void Start()
    {
        positionBefore = transform.position;


    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawCube(lastHit, new Vector3(.2f,.2f,.2f));
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 curruentMovement = transform.position;
        Vector2 dVector = (Vector2)transform.position - (Vector2)positionBefore;
        float distance = Vector2.Distance((Vector2)positionBefore, (Vector2)curruentMovement);
        RaycastHit2D[] hits = Physics2D.RaycastAll((Vector2)positionBefore, dVector, distance, collisionLayer);

        if(hits.Length > 0)
        {
            Debug.Log(hits.Length);
            for (int i = hits.Length -1 ; i >= 0 ; i--)
            {
                RaycastHit2D hit = hits[i];
                if (hit.collider != null)
                {
                    Debug.Log("hit");
                    lastHit.x = hit.point.x;
                    lastHit.y = hit.point.y;      
                    Vector3 resetPos = new Vector3(hit.point.x, hit.point.y, transform.position.z) + positionBefore.normalized * 0.1f;
                    transform.position = new Vector3(resetPos.x, resetPos.y, transform.position.z);
                }
            }
                
           
        }



        positionBefore = transform.position;
    }
}
question from:https://stackoverflow.com/questions/65844999/raycast2d-hits-only-one-side-of-collider

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

However, I am still wondering about the collision behavior of my raycast script. That would be surely interesting, if you want to calculate shots or similar via raycast

Alright, so your initial idea was to check if a collision had occurred, By checking its current position and its previous position. And checking if anything is between them, that means a collision has occurred. And you would teleport it back to where it was suppose to have hit.

A better way todo this would be to check where the GameObject would be in the next frame, by raycasting ahead of it, by the distance that it will travel. If it does hit something that means that within the next frame, it would have collided with it. So you could stop it at the collision hit point, and get what it has hit. (This means you wouldn't have to teleport it back, So there wouldn't be a frame where it goes through then goes back)

Almost the same idea but slightly less complicated.

Problem would be that if another object were to appear between them within the next frame aswell, it could not account for that. Which is where the rigidbody.movePosition shines, And with OnCollisionEnter you can detect when and what it collided with correctly. Aswell as without the need to teleport it back


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...