I've been working on a project involving instantiating an orb which then teleports the player to its location when hitting any object I tag with "floor". I am relatively new to programming and so any help is appreciated.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Floor")
{
script.canShoot = true;
isTriggered = true;
player.transform.position = teleportLocation;
Destroy(gameObject);
Debug.Log("hit floor");
}
My "teleportLocation" is just the orbs constant position as of currently.
Currently the issue I am facing is with moving the player, I have substituted the player for a cube which did work when setting the position. This is my script for instantiating the projectile:
Vector3 dir = mouse.transform.position - transform.position;
if (!disabled)
{
GameObject instance = Instantiate(orbPrefab, orbShooter.transform.position, orbPrefab.transform.rotation);
instance.GetComponent<Rigidbody>().AddForce(dir * power);
instance.transform.position = new Vector3(0, 0, 0);
}
The only way I have been able to teleport the player to the orb successfully was through this script:
public class teleport : MonoBehaviour
{
public Vector3 tpPos;
public GameObject testOrb;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
tpPos = testOrb.transform.position;
if (Input.GetKeyDown(KeyCode.E))
{
transform.position = tpPos;
}
}
}
I could move the player to an orb (which was not an instantiated object but was instead a normal game object moved by clicking) when I press "E" and this seemed to work. The problem doesn't seem to lie within the collision detection or transform positions but instead the instantiation. It would be helpful if anyone knows a way which could teleport my player to the instantiated orb.
the above involves the different things i had tried....