- Home /
Camera Drag Script not working as expected.
Here us the link of what am trying to achieve. Now the question that i am posting now is related to the scrolling of the upper section.Lets say we have around 30 planes in the upper section. and around 10-15 are loaded when the application start.Now as user swipe across the background new planes are generated and background is scaled along its X-axis.Now what i need is as i scroll and when around 30 elements are generated i want the scroll to stop there so that it doesn't go any further i.e showing the last plane on either side.Here is the link on this basis i have done my script.I created borders on either side and attached the second script to it,but don't know why its still scrolling.Pasting the script out here for the reference. I think the issue may be with the camera thing. But If anyone would point out if there are mistake somewhere else as well.Here is the ScrollElementScript
and this script is attached to the secondary camera.:
#pragma strict
var canMoveLeft: boolean = true;
private var dragSpeed: float = 0.05;
public var background:GameObject;
var dragCamera : Camera;
var startposition: Vector3;
var nextposition: Vector3;
var element:GameObject;
var count:int;
var testtext2:GameObject;
var testtext3:GameObject;
var Element: List.<GameObject>;
var rightborder:GameObject;
var canMoveRight: boolean = true;
function Start()
{
testtext3=GameObject.Find("TestText3");
testtext2=GameObject.Find("TestText2");
startposition=Vector3(31.36466,16.36855,15.13743);
Element.Capacity=20 ;
}
function Update ()
{
testtext3.GetComponent(TextMesh).text="Start Count::: "+count.ToString();
var ray = dragCamera.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if(hit.collider==background.collider)
{
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
if(Input.GetTouch(0).deltaPosition.x < 0 && canMoveRight || Input.GetTouch(0).deltaPosition.x > 0 && canMoveLeft)
{
if(Element.Count<21)
{
dragCamera.transform.position.x -= Input.GetTouch(0).deltaPosition.x * dragSpeed;
nextposition=startposition;
nextposition.x=startposition.x+4;
startposition=nextposition;
background.transform.localScale.x=background.transform.localScale.x+3.0;
testtext2.GetComponent(TextMesh).text="Updated Count::: "+Element.Count.ToString();
var go : GameObject = Instantiate ( element,nextposition,Quaternion.identity ); //no need for Resources.Load
Element.Add(go);
if(Element.Count==20)
{
rightborder=GameObject.Find("TopRightBorder");
rightborder.transform.position=nextposition;
}
}
}
}
}
}
}
function RestrictMove(dir: String)
{
if(dir == "left") canMoveLeft = false;
if(dir == "right") canMoveRight = false;
}
function UnRestrictMove(dir: String)
{
if(dir == "left") canMoveLeft = true;
if(dir == "right") canMoveRight = true;
}
Now the second script which are attached to the borders is EdgeScript
and it goes like this:
#pragma strict
var borderType: String = "top";
var dragCamera : Camera;
function OnBecameInvisible()
{
(dragCamera.gameObject.GetComponent("ScrollElementsScript") as ScrollElementsScript).UnRestrictMove(borderType);
}
function OnBecameVisible()
{
(dragCamera.gameObject.GetComponent("ScrollElementsScript") as ScrollElementsScript).RestrictMove(borderType);
}
I tried this procedure for other case and it worked perfectly.So where there can be an issue.
EDIT New observation that I encountered during further examination and testing.. Can scaling background might be another reason for it. ? The elements that are added are perfect 20 according to the condition. But its just the scroll that is not proper. Despite the part for Drag of camera being inside the condition it still moves even after satisfying the condition. And one more thing just out of curiosity while evaluating the condition just put the count of elements on the 3D Text
to find if the condition gets satisfied. As I was scrolling 20 elements are created even if I scrolled very little why is this so.I understand update method is called every frame and many times per second so it has to be with it. So is there any way of controlling this behaviour ?
Come On Guys no answers for two days ... thats strange.!
Your answer
Follow this Question
Related Questions
Can you use UI Scroll Rect for camera movement? 1 Answer
NGUI: Draggable Camera and Scroll Panel 0 Answers
The solution with camera rotation which is used in most strategic games 1 Answer
Rotating camera with RMB drag 1 Answer
How to have Camera following a Target, but not directly centered? 3 Answers