Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skeelons · Mar 18, 2016 at 12:18 PM · jumpingcharacter controllercamera follow

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

camera problem 0 Answers

Movement Script Causing Jump Upon Looking Up 2 Answers

My Character controller keeps jumping infinitely 2 Answers

Character controller jumping is unresponsive 0 Answers

Camera judders when I press jump in midair 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges