- Home /
teleport player using a interact script
so I made a interact script that when you press E will with in a box collider of the set object it triggers a script and teleports my player but nothing happens
Interact script :
public float interactDistance = 5f;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDistance))
{
if (hit.collider.CompareTag("portal"))
{
hit.collider.transform.GetComponent<TeleScript>().UseTele();
}
}
}
}
Teleport script:
public GameObject player;
public void UseTele()
{
player.transform.position = new Vector3(5.08f, 1.01f, -19.65f);
Debug.Log("TELEPORT");
}
Answer by V_0_1_D_ · Feb 27 at 12:09 PM
I tried and it work well. 1. Make sure your player's forward is towards the portal box. you can use Gizmos.DrawRay(transform.position, transform.forward * interactDistance);
to see the ray in the scene view. 2. Make sure the portal tag is assigned properly, it could be a mistake just like "Portal" instead of "portal".
Answer by Caeser_21 · Feb 27 at 04:14 AM
I dont think you need to use Raycasts...
you can use the "OnTriggerEnter" or "OnCollisionEnter" methods.
the ray cast work for all my other scripts (it helps set a distance), the problem is that its not moving the object to the new location
Your answer
Follow this Question
Related Questions
ping pong on x axis, unable to touch end of plane, 1 Answer
Need help updating camera position to go above the player 2 Answers
How do I get the transforms of all the objects in my scene ? 2 Answers
Check if there is a object in front of you and equal your velocity to that objects 0 Answers
How do i make my background keep the same position as my character? 0 Answers