Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calling a function from another script has problems

Discussion in 'Scripting' started by Hapciupalit, Dec 3, 2015.

  1. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Hello. So I was doing a Jump Function which if I call it from the same script is working but if I call it from another script it's not working well. For example I put some Debug.Log on the Jump function and when I call it from outsite the debug.log is showing but the player won't jump

    Code (JavaScript):
    1. private var moveDirection : Vector3 = Vector3.zero;
    2. var controller : CharacterController;
    3.  
    4. function Awake(){
    5.     controller = GetComponent.<CharacterController>();
    6.    
    7. }
    8.  
    9. function Update() {
    10.    
    11.     if (controller.isGrounded) {
    12.         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 1.0f);
    13.         moveDirection = transform.TransformDirection(moveDirection);
    14.         moveDirection *= speed;
    15.        
    16.         if(Input.GetButton("Jump")){
    17.         moveDirection.y = jumpSpeed;
    18.     }
    19.     }
    20.  
    21.     // Apply gravity
    22.     moveDirection.y -= gravity * Time.deltaTime;
    23.    
    24.     // Move the controller
    25.     controller.Move(moveDirection * Time.deltaTime);
    26. }
    27.  
    28. function Jump(){
    29.     if (controller.isGrounded) {
    30.         moveDirection.y = jumpSpeed;
    31.         Debug.Log("called");
    32.     }
    33. }
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    You call your jump function and it sets your Y speed. But your update function is setting moveDirection.y = 0.0f every frame.

    Unity has controllers that do all of this stuff you can look at and see how they do it. For instance you can use a Rigid Body and apply an Upwards force when you jump and the physics system will handle everything quite nicely.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    More to the point, your controller is still grounded by the time Update hits. So it's entering that if block, where you set moveDirection to a vector whose y is 0.

    A quick and dirty solution would be to add a "justJumped" bool - set it to true in Jump(), and check against it in addition to .isGrounded.
     
    Hapciupalit likes this.
  4. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Your dirty solution helped me alot . Thank you.
    Best Regards