Moving instantiated Objects to target locations
Hey Forums,
I got the following issue: I'm instantiating objects from a list of spawning points to 5 randomly picked locations and write them into an array. These Objects have triggers and can be clicked with the E key. When they get clicked my aim is to tranform their Position to 5 set objects in the world, where the order ist always 1, 2, 3, 4, 5. The order of clicking said instantiated Objects doesnt matter, they just get moved into the slots one by one.
I can't figure out how to get the clones to move to my target locations (Objects with different position)
Currently I have this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ImprisonBandits : MonoBehaviour
{
bool CheckE;
bool CatchBandit;
public int banditnumber;
public GameObject[] JailSpots;
public GameObject bandit;
void Start()
{
CatchBandit = false;
banditnumber = 0;
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player" && CheckE == true)
{
CatchBandit = true;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
CheckE = true;
}
else
{
CheckE = false;
}
if (CatchBandit == true)
{
bandit.active = false;
}
}
}
But sadly that doesnt quiet help. :/
Answer by lgarczyn · Jan 25, 2020 at 01:37 AM
Have a script that stores all position, as the transform components of markers.
public Transform[] storagePositions;
Have an index to know how many were stored
int storedCount;
Have a function to store an object
public void Store(Transform object)
{
if (storedCount >= storagePositions.Length)
{
Debug.LogError("Too many items stored");
return;
}
object.transform.position = storagePositions[storedCount].position;
object.transform.rotation = storagePositions[storedCount].rotation;
storedCount++
}
Either use a singleton to access this from your 'press e' script, or use a public reference, or use Find.
Your answer
Follow this Question
Related Questions
Object Reference not Set 1 Answer
Character picks up two cubes simultaneously 0 Answers
Rotation set to face direction of movement but doesn't always work 1 Answer
i need to change box position in y axis if i hit from down (c# script) 2d 1 Answer
Translating an Object to a Random Endpoint,Moving an Object to a random endpoint 0 Answers