- Home /
Infinite jumping problem: Jump function (kinda) stops calling when camera follow script applied
Hey, so I am new to unity and have no background in coding, which means that to build the game that I've been working on for the last few months, I've been pretty much extensively researching until I find a tutorial that will walk me through creating whenever aspect of the game I'm working on (thankfully so far, I've been sticking to incredibly common gameplay features.) Unfortunately, this also means that when something stops working I get completely stumped.
Here's the issue I'm working on now: in my 2D pixel game, I'm using the code from the 2D Character Controller tutorial on the unity (code below), but in some scenes I also have a script that has the camera follow the player while staying within the boundaries of the scene from another tutorial (code also below). Like it says in the title, whenever this script is applied the jump feature in my other script seems to break. It does not call the jumping animation, nor does it seem to recognize when the player is "grounded" anymore. When I check the animator I can see that the jump animations are no longer activated when I jump. HOWEVER, the force to jump is still applied, which enables the character to infinitely jump off the screen when the space bar is pressed. As I use both these scripts for platforming elements of my game, it's important that I fix this somehow.
Character Controller Script:
public class CharacterController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
private Rigidbody2D playerbody;
Animator anim;
bool grounded = false;
public Transform groundCheck;
public LayerMask whatIsGround;
public float jumpForce = 700f;
// Use this for initialization
void Awake ()
{
playerbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
Vector2 playerPos = new Vector2(transform.position.x, transform.position.y);
Vector2 groundPos = new Vector2(groundCheck.position.x, groundCheck.position.y);
//I'm using a raycast method instead of the circle one in the tutorial
grounded = Physics2D.Linecast (playerPos, groundPos, whatIsGround);
anim.SetBool ("Grounded", grounded);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
playerbody.velocity = new Vector2 (move * maxSpeed, playerbody.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update()
{//I know I should remap this to not be hardcoded, I just haven't researched it yet haha
if (grounded && Input.GetKeyDown ("space"))
{
anim.SetBool ("Grounded", false);
playerbody.AddForce (new Vector2(0, jumpForce));
}
}
//making sure the character faces the right way, etc
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Camera follow script:
public class CameraFollow : MonoBehaviour {
public Transform Player;
public Vector2 Margin;
public Vector2 Smoothing;
//I have a box collider encompassing the whole boundary of the level here, set to it's own "ignore" layer
public BoxCollider2D Bounds;
private Camera _camera;
private Vector3 _min;
private Vector3 _max;
public bool IsFollowing { get; set;}
public void Start()
{
_camera = GetComponent<Camera> ();
_min = Bounds.bounds.min;
_max = Bounds.bounds.max;
IsFollowing = true;
}
//Using the camera's orthographic size to find the screen area, calculating and smoothing the camera follow
public void Update()
{
var x = transform.position.x;
var y = transform.position.y;
if (IsFollowing) {
if (Mathf.Abs (x - Player.position.x) > Margin.x)
x = Mathf.Lerp (x, Player.position.x, Smoothing.x * Time.deltaTime);
if (Mathf.Abs (y - Player.position.y) > Margin.y)
y = Mathf.Lerp (y, Player.position.y, Smoothing.y * Time.deltaTime);
}
var cameraHalfWidth = _camera.orthographicSize * ((float)Screen.width / Screen.height);
x = Mathf.Clamp (x, _min.x + cameraHalfWidth, _max.x - cameraHalfWidth);
y = Mathf.Clamp (y, _min.y + _camera.orthographicSize, _max.y - _camera.orthographicSize);
transform.position = new Vector3 (x, y, transform.position.z);
}
}
The only thing I can think of is that the scripts are interfering with each other somehow because they both call on my player object. Is that an issue somehow? Is this a quick fix at all, or will I need to look into different ways to structure these elements? I'm very out of my depth here.
Your answer
Follow this Question
Related Questions
camera problem 0 Answers
Movement Script Causing Jump Upon Looking Up 2 Answers
My Character controller keeps jumping infinitely 2 Answers