- Home /
I want instantiated object to stick with in-scene object
public float msslSpeed;
private Rigidbody rb;
public ForceMode mode;
private bool isFired = false;
private Vector3 offset;
void Start()
{
rb = GetComponent<Rigidbody>();
offset = transform.position - PlayerController.player.transform.position;
}
void FixedUpdate()
{
Fire();
StickOnto();
msslTravel();
}
private void StickOnto()
{
if(isFired == false)
{
transform.position = PlayerController.player.transform.position + offset;
}
}
private void Fire()
{
if (Input.GetButton("Fire2"))
{
isFired = true;
}
}
private void msslTravel()
{
if (isFired == true)
{
rb.AddForce(Vector3.forward * msslSpeed, mode);
Destroy(gameObject, 8);
}
}
It is my missile's code that in prefab. But I can't use my player object since it is already in the scene. Is there any other way to make it stuck?
Can you please be a little more descriptive. What is the exact behaviour you are trying to achieve.
Answer by foshe_5 · Aug 24, 2019 at 12:51 PM
I don't really understand what you're trying to say, but if you want to reference the player object on another script then you can use: FindObjectOfType(Player).transform.position
Again I'm not sure if that's what you want
I attached this code to prefab. And this prefab is instantiated when it's loaded. After loading this, I want it to stuck at loaded point untill it gets fired.
Your answer
Follow this Question
Related Questions
Why Transform and not Prefab? 2 Answers
transforming a prefab randomly at runtime 3 Answers
Saving customized transform in game 1 Answer
Parenting GameObjects 1 Answer
Dragging prefab into hierarchy reset position now? 0 Answers