- Home /
How to make an object move after teleporting it to a specific location
Hi there!
I have a character which can move with the keys and I have a script attached to it which teleports it to a specific location. But after it has teleported it is stuck in this location and can't move anymore. How can i make my character move after being teleported, stop it from being fixed to the position? Sorry if this topic has already been discussed, i couldn't find anything on it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleportation : MonoBehaviour {
private Vector3 destination;
Rigidbody rb;
public float speed;
public float jumpPower;
// Use this for initialization
void Start () {
destination = new Vector3(4.70f, 2.9f, 4.699f);
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed * Time.deltaTime);
Invoke("TeleportToDestination", 1f);
}
void TeleportToDestination()
{
transform.position = destination;
}
}
Answer by dishant27 · Feb 28, 2018 at 07:27 PM
You are calling the TeleportToDestination() method in FixedUpdate. So it's after 1sec of gameplay, Invoke method is called again and again which is responsible for the problem. Call it from start method or using OnTriggerEnter on the position from where you want to teleport.
Thanks dude!
How would i go about if i had to collect a number of objects before i can teleport. If i set the teleport method in "start" it will only be called once in the start of the game...
hmmm
Place colliders on the places from where you want to teleport. Call the TeleportToDestination() from the OnTriggerEnter method. https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers