- Home /
how to stop the camera to follow the player on his jump movement ?
So the player constantly move on a diagonal direction, and the camera follow him (on x and y, it's 2d gameplay) But when the player jump, i want the camera continue to follow him but not following him on his jump movement. I don't if it's clear ! :/ Is there a way to achieve that ? Edit : my first atempt was that, but cause the player is always moving, it's not working perfectly
using UnityEngine;
using System.Collections;
public class cameraScript : MonoBehaviour {
public Transform target;
public CharacterController controller;
private int offSetz = -100;
public float offSetx;
public float offSety;
private float lockPos;
void Start()
{
}
void Update()
{
if (controller.isGrounded)
{
transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
lockPos = target.transform.position.y;
}
else {
transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
}
}
}
have you some script that controls camera? just disable or clamp there it's Z position
Cause the player continue moving on y axis even if he is not jumping. It's a diagonal direction, so the camera stop folowing him on y, but the player still move on this axis. I want the camera to just don't move when he is jumping, but still following him on his base movement.
Answer by RodrigoSeVeN · Aug 05, 2012 at 04:51 PM
I think I understood. This is more of a suggestion than the best solution.
As far as I get it, you need to keep the camera's x axis aligned with the player at all times, and only when he jumps, you need the y axis to stay on the surface of the ground, instead of "jumping" with the player.
The best suggestion I have, is to fire a ray from the player position, only when he is jumping, towards the ground, so you can get a position on the ground to use, instead of the y axis of the player while he is jumping.
Before putting any code, this system should work like this:
You fire the ray during the player jump(when not grounded). You will get a vector3(the hit.point), but you will only need the y value. To make it work correctly, you need to add another value to this result, that being the approximate height of the player(this height is actually the distance of his y position in relation to the ground he is touching, let me know if this is not clear for you).
You take this y value and assign it to the camera position, and the camera should move diagonally on the ground, while the player is jumping.
Now for the related code. You can call a function on the camera script that only sets the y value when the player is jumping(let the camera know when it happens). Also, I'm considering your camera already have the player as a target and that your ground has a collider, so here you go.
(I only work with Javascript, but it won't be hard to translate. In case someone feels like doing so, please edit this code and let us know you did it.)
var target:Transform; //The player
var pHeight:float=0; //The player "height", play with this to see if you need it
function AlignIfJumping(){
var hit:RaycastHit;
if(Physics.Raycast(target.position, -Vector3.up, hit)){
var tempYPos:float=hit.point.y+pHeight;
transform.position.y=tempYPos;
}
}
Remember, you need to call this function on the camera, only when the player is jumping and right after the camera do it's auto alignment with the player position, so the y axis position is changed correctly before the frame ends.
Yeah you understood it well ! Thx you man, it's working ! If i could i would "thumb up you"... but i can't... ^^ thx again !
Answer by Seth-Bergman · Aug 05, 2012 at 02:16 PM
ok.. if it's just while jumping that's the issue, you need a separate var for that, something like:
void Update()
{
if(Input.GetButton("Jump"))//or however you choose to determine this based on your code
jumping = true;
if (jumping)
{
transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
lockPos = target.transform.position.y;
}
else {
transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
}
if(controller.isGrounded) // may need to adjust this also.. just an example
jumping = false;
}
Of course, this all depends on when your camera should or shouldn't follow on y. If there is a certain scenario that is the issue, you could conversely simply account for that in a similar way, something like:
if (!diagonalMove && !controller.isGrounded)
transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
else
{
transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
lockPos = target.transform.position.y;
}
Your answer
Follow this Question
Related Questions
Getting Camera to Stop Following Player 1 Answer
Camera rotation around player while following. 6 Answers
Mouse Orbit + Move Object + Follow Problem 1 Answer
Camera not moving on mobile 0 Answers