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.
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.