How would I fix this clipping issue

I’m trying to make a teleport system and can’t get it to work how I want. How would I make it so my model doesn’t glitch into collider.

using System;
using UnityEngine;

public class Teleport : MonoBehaviour
{
    public LayerMask layer;

    private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            MovePlayer();
        }
    }

    private void MovePlayer()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, layer) == true)
        {
            transform.position = hit.point;
        }
    }
}

i would get the normal of what your ray hit, multiply that by how far away from the hitpoint you want to teleport your object to then set your position to be be that hit point position + that value.

something like this

transform.position = hit.point + hit.normal * dist;

Exactly what I was looking for, you are a saint. Thank you. Also would there be a way to make the teleport have a delay, so I could see my model moving instead of it instantly going there.

Vector3.Lerp should be able to help you with that, but you will either need to move it in the update 1 frame at a time or in a Coroutine. If you want a off the shelf way of do that in 1 method call i would get DoTween to do it.

If you have the time is there a chance you could show me how to implement that in code? I’m kind of new to this

if i was going to do it, without adding a library like DoTween i would make a coroutine like this.

private IEnumerator PositionOverTime(Vector3 destination, float duration) {
    var start = transform.position;
    var elapsed = 0f;

    while (elapsed < duration) {
        elapsed += Time.deltaTime;
        var t = elapsed / duration;
        transform.position = Vector3.Lerp(start, destination, t);
        yield return null;
    }

    transform.position = destination;
}

you can later call that using StartCoroutine

StartCoroutine(PositionOverTime(destination, 1f));

and that would move it over 1 second of time

this is the simplest form, i would likly also add a easing function to it, or use a AnimationCurve to smooth out t.

other thing of note, when you call StartCoroutine, you might want to cache what it returns so you can later stop the Coroutine if needed.