- Home /
Camera follow an object on just z-axis.
Hello, i am working on a bike came and i want to move my camera which just follow my bike. i am using smooth follow script but when i change my lane, my camera also change its position to x axis. which i do not want. i have tried to make changes in SmoothFollow script but could get to the right direction.
can anybody give me some hint to to achieve that as i am new in unity.
thanks
public class CaneraFollow : $$anonymous$$onoBehaviour {
public Transform player;
[Range(0.01f, 1.0f)]
public float SmoothFactor = 0.5f;
public Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.position;
}
// Update is called once per frame
void LateUpdate () {
Vector3 desiredPosition = player.position + offset;
desiredPosition.x = 0f;
desiredPosition.y =0f;
transform.position = Vector3.Slerp(transform.position,desiredPosition,SmoothFactor);
}
} With this code, your camera will only follow your player on the z-axis.
Answer by Marnix · Jun 24, 2011 at 12:28 PM
Instead of following your bike, your camera needs to follow some stub. The stub doesn't change lanes. So you need to let the stub update the positions of the bike in only 2 directions and let the third out. This way, you don't need to change the smoothFollow script.
Thanks $$anonymous$$arnix i have done it with empty game object.
Answer by Anxo · Jun 24, 2011 at 12:38 PM
var target : Transform;
var distance : float = 10.0;
function Update(){
transform.position.z = target.position.z - distance;
}
Answer by sanghai · Sep 07, 2011 at 02:31 PM
Hey Sajjo I have the same problem and I did not understood what Marnix is trying to say May i get the help for the same ?
I am trying to make game like Air Strike 3D where camera moves along in z axis but if plane goes in left then screen or say camera also scrolls along x axis for about 5 meter....same shd happen for right
Please help me regarding the camera view....
Thanks in advance
Ok, create an empty game object.
Place your camera and your player within said game object.
Rotate your camera to the view you want.
Attach the asset "SmoothFollow2D.js" to your camera and assign the player as the target.
This should work.
Answer by sanghai · Sep 08, 2011 at 05:39 AM
Thanks taddmencer for your instant reply but this is not what I want just check out Air Strike 3D game where camera is not working as smooth flow to player but something else I am not able to figure that out.
I will give you link of youtube please check this and if u will be able to help me i will be very helpful
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 BudBroesky · Jun 24, 2014 at 04:46 AM
Use this on you main camera, i am very new to c# but I think I made a stream-lined, efficient script to follow any player on ONLY the z-axis.
using UnityEngine; using System.Collections;
public class CameraBehaviors : MonoBehaviour {
public Transform[] player;
private float playerZ;
private Vector3 follow;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
playerZ = player[0].position.z - 2;
follow = new Vector3(0f,8.23f,playerZ);
transform.position = follow;
}
}
The part where I describe the Y axis for "follow" it just how high you want the camera, that can be changed to your liking. also the -2 in "playerZ" it just so the camera is slightly behind the player.
Hope this script helped!
Your answer

Follow this Question
Related Questions
Can Unity 3 build for the iOS Simulator? 2 Answers
Rendering issue on iPhone 1 Answer
Help Basketball Shooting Game 0 Answers
Camera Follow - SmoothDamp Inconsistency 0 Answers
Porting Unity Web Player game to iPhone using Unity 1.7 iPhone 1 Answer