i want the camera stop follwing my player only on y axis when he jumps...

Hi everyone. Im new to scripting. Im writing this code for my player. It moves great, it jumps well, and ive attached the camera to my player to be a child of it, but the problem is that i dont want the camera to follow the player when he jumps. Actually i want the camera to remain on where it is right now on its y axis but still be a child of my player and follow it on x axis and i have to mention that its a 2d game and ive attached this script to my player. I really need your help. tnxx alot…

var playerSpeed : int;
var jumpHeight  : int;

function Update () 
{
    //1- childing the main camera to the player, so that it will follow the player transform.position
    // getting the transform of the main camera and put it in a new var.
    var cameraTransform = Camera.main.transform;
    
    // making the camera child of the player.
    cameraTransform.parent = transform; 
    
    //2- moving the player...
    // amount to move the player 
    amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    
    // transform/translate the movement of the player...
    transform.Translate(Vector3.right * amtToMove);
    
    //3- player jump
    // player can countinusly jump when you hold the space. if its transform position in y axis is equal to 0.73(on the ground)
    if ( Input.GetKey("space") && transform.position.y == 0.73)
    {
        amtToJump = jumpHeight + Time.deltaTime;
        transform.Translate(Vector3.up * amtToJump );
    }
 
    // now here i want to add something that can prevent the camera to follow my player when he jumps! and i need your help :(

}

Hello,
what you can do is take the camera out of the player. Don’t put it as a child of player.
Inside the camera you can put a script that will follow the player only in X axis:

C#- Script

using UnityEngine;
using System.Collections;

public class FollowPlayer : MonoBehaviour {
 
 public GameObject player;
 
 // Update is called once per frame
 void Update () {
 
 this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x,this.gameObject.transform.position.y,this.gameObject.transform.position.z);
 
 }
}

Javascript:

public var player : GameObject;

function Update () {

 this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);

}

After you drag the script inside the camera, just reference the player object.

Hope this helped you.
Take care!

oh here we go :smiley: i fixed it myself! :stuck_out_tongue:
i just added this function out of my update function after parenting the camera to the player,and just changed the value to what works…easyy :

function LateUpdate()
{
    Camera.main.transform.position.y = 2.77;  
}

now theres another problem! i want the player rotate when i press for example left arrow,and countinue moving to the left direction.just like old super mario movement . when u press left it would rotate and walk,and when u hold right arrow it could walk at the opposit direction.now i want something like that.i wrote some roatation code but when i hold the left or right arrow it just rotates and stops moving.and also my camera is a child of my player,and when player rotates the camera rotates too! i want to stop that too. really need your helpp. tnx

hey guys realy apreciate for your helps but i think im done with the camera section,as i mentiond earlear by using the late update function it works well right now.

AND also tnx alot BLarentis but still it doesnt work :((
let me give u my latest player script:

var playerSpeed : int;

var jumpHeight : int;

function Update ()
{

//1- childing the main camera to the player, so that it will follow the player transform.position

// getting the transform of the main camera and put it in a new var.

var cameraTransform = Camera.main.transform;

// making the camera child of the player.

cameraTransform.parent = transform;

//2- moving our player…

// amount to move the player

 amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;

// transform/translate the movement of our player…

 transform.Translate(Vector3.right * amtToMove);

//------------------------------------------------------------------------

//3- player rotation…!

 if (Input.GetKey("left") || Input.GetKey("a"))
   {
       this.gameObject.transform.rotation = Quaternion.Euler(0,0,0); 
   }
        
 if (Input.GetKey("right") || Input.GetKey("d"))
   {
       this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);
   }

//--------------------------------------------------------------------------

//4- player jump

// player can countinusly jump when you hold the up button. if its transform position in y axis is equal to 0.73(on the ground)

if ( Input.GetKey(“up”) && transform.position.y == 0.73)
{

    amtToJump = jumpHeight + Time.smoothDeltaTime ;
    
    transform.Translate(Vector3.up * amtToJump );
    

  }

}

//5- camera follow

// this function lets the camera be parented to the player but prevent it from moving along the y axis when the player jumps.

function LateUpdate()

{

Camera.main.transform.position.y = 2.77;

}

now i can move,jump,and the camera works well.but im facing new problems . its been about a day that im thinking to fix this.now i cant rotate, even if i rotate the camera rotates too.but i dont want that happen.and also ive got a cube like wall and i want my player to stop passing through it when it hits the wall.now i have box colider on both my player and wall with is trigger off,and have rigid body with use gravity on and is kinemati off on my player and wall. i think they colide cuz the wall doesnt move but my player suddenly jumps to the sky when it hits the wall! i dont know whats the problem. really need your help.and really apreciate that.
and by the way :smiley: forgive me for my bad english ! :wink:

the script I used would always snap to a set position, and I tried many things to fix it.
In the end I just tried using this script:

function LateUpdate () {
    transform.position.y = 3.05;
    transform.position.z = -6.94;
}

Make sure it is a late update or else it will not work!
When the object your the camera is following, the camera should remain in the place you said in the script, but the x axis will still move

(I also made the axis as 3.05 and -6.94 as that because that was the right camera positon)