- Home /
passing variables to control Camera position
Hello All,
I am moving a camera (myCamObj) and when I click on something (script #1) I record the cameras Vector3 Pos in a public variable (camPos)
Then it moves over and follows a path and then when it clicks on something else (script #2) I want it to return to the original place it left (which would still be 'camPos")
I also need to reset its rotation to forward
The variables are not being passed nor is the rotation realigning
Script #1
using System.Collections;
public class iTweenMouseUpPath : MonoBehaviour {
public GameObject myCamObj;
bool myTrigger = false;
public Transform myQuad;
// for use in another script
public Vector3 camPos;
//On mouseUp put camera on path
void OnMouseUp () {
//Record Camera Pos to be called by another script
camPos= myCamObj.transform.position;
Debug.Log ("camPos = " + camPos);
//Then Move Camera
//iTween.MoveTo(myCamObj, iTween.Hash("path", iTweenPath.GetPath("WLKPath"), "time", 15));
iTween.PutOnPath(myCamObj, iTweenPath.GetPath("WLKPath"), 0);
//Triger the look at in the Update Function
myTrigger = true;
}
//Keep Cam looking at an Object
void Update()
{
if (myTrigger == true)
{
myCamObj.transform.LookAt (myQuad);
}
}
}
Script #2:
using UnityEngine;
using System.Collections;
public class mouseUpBack2 : MonoBehaviour {
public GameObject myCamObj;
public Vector3 camPos;
void OnMouseUp () {
//Debug.Log ("camPos2 = " + camPos);
myCamObj.transform.position = camPos;
myCamObj.transform.rotation = Quaternion.Euler(0,0,0);
}
}
Thanks !
~be
Answer by wangunity · Jan 27, 2015 at 09:15 AM
I remember you answered my question.hehe...so coinsidence. if you want to assign value with campos. you should in the script 2 to get the first script, and obtain the campos value. below is a simple code. public class mouseUpBack2 : MonoBehaviour {
public GameObject myCamObj;
public Vector3 camPos;
public iTweenMouseUpPath _itweenpath;
void OnMouseUp () {
//Debug.Log ("camPos2 = " + camPos);
//IF the _itweenpath attach the current gameobject.
_itweenpath = getcomponent<_itweenpath>();
myCamObj.transform.position = _itweenpath.campos;
myCamObj.transform.rotation = Quaternion.Euler(0,0,0);
}
}
wish to help you
Thank you wangunity
I didn't se your response until this A$$anonymous$$. Yes coincidence : )
I was asking a more general question about passing variables, but if this works switching iTween paths it might be very nice for me
I will check this now, Thanks!
~be
Hello
This isn't working for me yet. I get the following errors
Assets/mySCRIPTS/mouseUpBack2.cs(19,44): error CS0118: mouseUpBack2._itweenpath' is a
field' but a `type' was expected
Thanks again
~be
So I modded the script
I'm starting to get it , but
I get the following Error in Console:
NullReferenceException: Object reference not set to an instance of an object mouseUpBack2.On$$anonymous$$ouseUp () (at Assets/mySCRIPTS/mouseUpBack2.cs:20) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
But I can see from the Debug.Log that camPos is being passed correctly The problem seems to be in the second to last line:
myCamObj.transform.position = camPosScript.camPos;
Both sides of the equation are vector3's I'm simply stating the camera position to the position held in the passed variable
using UnityEngine;
using System.Collections;
public class mouseUpBack2 : $$anonymous$$onoBehaviour {
public GameObject myCamObj;
public Vector3 camPos;
public iTween$$anonymous$$ouseUpPath camPosScript;
void On$$anonymous$$ouseUp () {
Debug.Log ("camPos2 = " + camPosScript.camPos);
camPosScript = GetComponent<iTween$$anonymous$$ouseUpPath> ();
myCamObj.transform.position = camPosScript.camPos;
myCamObj.transform.rotation = Quaternion.Euler(0,0,0);
}
}
Thanks!
~be
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to add a variable to a prebuilt class? 1 Answer
Class Scope Variable 1 Answer
How to pass variables from one object in one scene to another object in another scene? 3 Answers