#[SOLVED] Character Last Position

1 messages · Page 1 of 1 (latest)

jaunty topaz
#

here's my Script

using System;
using Unity.VisualScripting;
using UnityEngine;

public class CharacterRaycast : MonoBehaviour
{
    public Vector2 direction;
    public float distance = 0.5f;
    public LayerMask layerMaks;
    
    private Vector2 lastPosition;
    private bool isHit = false;
    

    void Update()
    {
        //Cast a ray in the direction specified in the inspector.
        RaycastHit2D hit = Physics2D.Raycast(this.gameObject.transform.position, direction, distance, layerMaks);

        Debug.Log($"lastPosition : {lastPosition}");
        //If something was hit.
        if (isHit) return;
        if (hit.collider != null)
        {
            //Display the point in world space where the ray hit the collider's surface.
            Debug.Log(hit.point);
            isHit = true;
            lastPosition = hit.transform.position;
            Debug.DrawRay(transform.position, direction, Color.green);
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"Hit");
        Debug.Log($"name : {other.name}");
        Debug.Log($"other : {other.gameObject.layer == LayerMask.NameToLayer("Player")}");
        if (other.CompareTag("Hole"))
        {
            transform.position = new Vector3(lastPosition.x, lastPosition.y);
        }
    }
}

#

i don't see anything wrong in here but when my character got hit by "Hole" (LayerName). my character position become weird. like in video.

#

how can i make something like this?

lilac basin
#

Or hit.point

#

Because hit.transform.position can be way off, depending on where that transform's pivot is

jaunty topaz
jaunty topaz