- Home /
Need help for camera view like Air Strike 3d game ?
Hello Everyone,I am beginner for unity and trying to make game of aeroplane like Air Strike 3d ........ I need help in camera section. I want to move my cam just like in that game where it is following the Airplane along x axis as well as when air plane goes to left it scrolls on left for say 100 m and same goes with right
I dont think its smooth follow because it is not following airplane i guess ... Please please help me with the soultion
I tried making a new empty game object and drag both camera and airplane in it ...and then tried smooth follow script but its not the way i wanted..
I am sory if i could not explain the problem properly here is the link of the Air Strike 3d
this is the link http://www.youtube.com/watch?v=zHkARvzsrMU
check the video after 4.42 minutes to avoid the information screen and all..
See in that screen scrolls little bit left ...right and i dont think its smooth follow if it is then please provide me the solution....if possible i will be very thankful...
Answer by Nonakesh · Sep 09, 2011 at 09:47 AM
I don't think the camera is following the player along the x (I don't think its the x-axis, but you know what I mean^^), but the player is following the camera! (he is limited how far he can go forward and backward depending on the position of the camera)
Ok... now the scrolling.... I think the camera starts scrolling as soon as the player is at certain distance to the player.
if(Math.Abs(Camera.main.transform.position - transform.position) > scrollDistance){
//start moving camera
}
Answer by sanghai · Sep 09, 2011 at 11:19 AM
Thanks dude you made my day ... ( by the way i understood what you meant ^-^) .............Sorry
By the way I have done this and it is working but my when my airplane goes to left it goes smoothly but when i try to come back again to the normal position or to d right
Airplane gets jerk or shock .... ( understood what i mean ....:) )
what should i do about that jerk .... ?
var scrollDistance:float =100.0;
private var endtarget=100.0; working
function Update(){
/*transform.position.y=currentHeight;
transform.position.z = target.position.z - distance;*/
if(Mathf.Abs(Camera.main.transform.position.x - transform.position.x) > scrollDistance)
{
//start moving camera
Camera.main.transform.position.x= transform.position.x + endtarget;
}
}
O$$anonymous$$! Sry for the late answer.... but you really should have used the comment button, ins$$anonymous$$d of answer!
This should work:
var scrollDistance:float =100.0;
var scrollSpeed = 1;
function Update(){
transform.position.x += Input.GetAxis("Horizontal");
/*transform.position.y=currentHeight;
transform.position.z = target.position.z - distance;*/
if($$anonymous$$athf.Abs(Camera.main.transform.position.x - transform.position.x) > scrollDistance)
{
Camera.main.transform.position.x += scrollSpeed * $$anonymous$$athf.Sign(Camera.main.transform.position.x - transform.position.x);
}
}
btw... don't forget to press "Correct answer" ;D
THAN$$anonymous$$ YOU VERY $$anonymous$$UCH ............. O ya i sorry for not using comment button ...but now I did !!!
yeah... you made it REALLY the right way... you pressed correct answer on you own answer, not on $$anonymous$$e.. :/
Hello ..... need help again ... I run your script its working properly but now its constantly following the plane in center....If you will see the Air Strike 3D game its behaving like ... If plane is in center and after some distance say 20 meters to the left then camera also scrolls to the left for the same distance around and then it comes to the distance where camera stops to the extent left but not the plane .... Here its not following the plane in center ... whereas camera moves first ...
I have made this script but dont know its not working ... I have just done for the left for right now Please check
public float scrollDistance = 100.0f; public int scrollSpeed = 1; private float x_Scroll_Left; private float x_Scroll_Right; public float smoothTime = 0.3F; private float yVelocity = 0.0F; private float newPosition;
// Use this for initialization void Start () { $$anonymous$$oveAirPlane someScript; someScript = GetComponent<$$anonymous$$oveAirPlane>();
x_Scroll_Left= (someScript._init_x - someScript._horizontal_extent)/2; x_Scroll_Right = someScript._init_x + someScript._horizontal_extent;
}
// Update is called once per frame void Update () {
//Debug.Log("co$$anonymous$$g ");
if (transform.position.x >= x_Scroll_Left)
{
//float newPosition = $$anonymous$$athf.SmoothDamp(Camera.main.transform.position.x,(transform.position.x + 100), ref yVelocity, smoothTime);
// Camera.main.transform.position.x += scrollSpeed * $$anonymous$$athf.Sign(Camera.main.transform.position.x - transform.position.x);
// Camera.main.transform.position.x += scrollSpeed * $$anonymous$$athf.Sign(Camera.main.transform.position.x - transform.position.x);
//float newPosition = Camera.main.transform.position.x smoothTime;
newPosition += scrollSpeed * $$anonymous$$athf.Sign(Camera.main.transform.position.x - transform.position.x);
Camera.main.transform.position = new Vector3(newPosition, Camera.main.transform.position.y, Camera.main.transform.position.z);
//Camera.main.transform.position = new Vector3(scrollSpeed * transform.position.x,Camera.main.transform.position.y,Camera.main.transform.position.z);
}
After playing for so much time now I might guess that camera is not following the airplane because Air Plane is moving freely even after camera scrolls without camera being moved .... please help ...thanks in advance