- Home /
When camera is set to active: transform gameobject forwards from same transform position not working (help)
Hi, when a camera is set to active I would like a plane to start moving forwards from a certain position. When it is set to inactive again, I would like the plane to stop moving and be transformed to the start position. (and if the camera is later set to active again, it to then start moving forwards again).
I first tried this script, which I attached to the plane:
var plane : GameObject;
var cameraC5 : GameObject;
function Update () {
if(cameraC5.activeInHierarchy == true) {
plane.transform.position = Vector3 (8.6, 22.7, -22.5);
rigidbody.velocity = transform.forward * 4;
}
if(cameraC5.activeInHierarchy == false) {
rigidbody.velocity = transform.forward * 0;
}
}
But this didn't work, as the plane stayed in the same position. I then tried this script, which i attached to the camera:
var plane : GameObject;
function OnEnable()
{
StartCoroutine ("planeTransformCoroutine");
}
function OnDisable()
{
StopCoroutine ("planeTransformCoroutine");
plane.transform.position = Vector3 (8.6, 22.7, -22.5);
}
function planeTransformCoroutine ()
{
//plane.active = true;
plane.transform.forward * 3;
}
But the plane still doesn't move!
Do you know what I'm doing wrong?
All the best, Laurien
Answer by jenci1990 · Jan 25, 2015 at 05:57 PM
I change the first script. Try it:
var plane : GameObject;
var cameraC5 : GameObject;
var planeSpeed = 1f;
function Update () {
if(cameraC5.activeInHierarchy == true) {
plane.transform.position += plane.transform.forward * Time.deltaTime * planeSpeed;
}
if(cameraC5.activeInHierarchy == false) {
plane.transform.position = Vector3 (8.6, 22.7, -22.5);
}
}
Your answer

Follow this Question
Related Questions
function behaves unexpectedly inside a coroutine. 2 Answers
bounce player back to position while jumping 0 Answers
Move player left or right after x seconds 1 Answer
Static Coroutine being called endlessly 2 Answers
Code working on wrong axis. 1 Answer