Search Unity

Problem with AddForce

Discussion in 'Scripting' started by TiredCoder, Jan 11, 2014.

  1. TiredCoder

    TiredCoder

    Joined:
    Jan 11, 2014
    Posts:
    8
    I am making a game for Android and I am using the Mobile Standard Assets' Side Scroller script because I am relatively new to unity and programming. The problem is that I can't understand some parts of the script thus making it difficult for me to find the errors. If someone could help me I would be so glad.
    PD: I am using JavaScript.
    Code (csharp):
    1. #pragma strict
    2.  
    3. @script RequireComponent( CharacterController )
    4.  
    5. // This script must be attached to a GameObject that has a CharacterController
    6. var moveTouchPad : Joystick;
    7. var jumpTouchPad : Joystick;
    8.  
    9. var moveRight : KeyCode;
    10. var moveLeft : KeyCode;
    11. var jumpKey : KeyCode;
    12.  
    13. var forwardSpeed : float = 4;
    14. var backwardSpeed : float = 4;
    15. var jumpSpeed : float = 16;
    16. var inAirMultiplier : float = 0.25;                 // Limiter for ground speed while jumping
    17.  
    18. private var thisTransform : Transform;
    19. private var character : CharacterController;
    20. private var velocity : Vector3;                     // Used for continuing momentum while in air
    21. private var canJump = true;
    22. private var x : float;
    23.  
    24. function Start()
    25. {
    26.     // Cache component lookup at startup instead of doing this every frame     
    27.     thisTransform = GetComponent( Transform );
    28.     character = GetComponent( CharacterController );   
    29.     x = transform.localScale.x;
    30.  
    31.     // Move the character to the correct start position in the level, if one exists
    32.     var spawn = GameObject.Find( "PlayerSpawn" );
    33.     if ( spawn )
    34.         thisTransform.position = spawn.transform.position;
    35. }
    36.  
    37. function OnEndGame()
    38. {
    39.     // Disable joystick when the game ends 
    40.     moveTouchPad.Disable();
    41.     jumpTouchPad.Disable();
    42.  
    43.     // Don't allow any more control changes when the game ends
    44.     this.enabled = false;
    45. }
    46.  
    47. function Update()
    48. {
    49.     var movement = Vector3.zero;
    50.  
    51.     // Apply movement from move joystick
    52.     if ( moveTouchPad.position.x > 0 || Input.GetKey(moveRight)){
    53.         transform.localScale.x = x;
    54.         movement = Vector3.right * forwardSpeed * 2 /*moveTouchPad.position.x*/;
    55.     }
    56.     else if(Input.GetKey(moveLeft) || moveTouchPad.position.x < 0) {
    57.         transform.localScale.x = -x;
    58.         movement = Vector3.right * backwardSpeed * -2 /*moveTouchPad.position.x*/;
    59.     }
    60.     // Check for jump
    61.     if ( character.isGrounded )
    62.     {      
    63.         var jump = false;
    64.         var touchPad = jumpTouchPad;
    65.            
    66.         if ( !touchPad.IsFingerDown() || Input.GetKey(jumpKey))
    67.             canJump = true;
    68.        
    69.         if ( canJump  Input.GetKey(jumpKey) /*touchPad.IsFingerDown()*/ )
    70.         {
    71.             jump = true;
    72.             canJump = false;
    73.         }  
    74.        
    75.         if ( jump )
    76.         {
    77.             // Apply the current movement to launch velocity       
    78.             velocity = character.velocity;
    79.             velocity.y = jumpSpeed;
    80.         }
    81.     }
    82.     else
    83.     {          
    84.         // Apply gravity to our velocity to diminish it over time
    85.         velocity.y += Physics.gravity.y * Time.deltaTime;
    86.                
    87.         // Adjust additional movement while in-air
    88.         movement.x *= inAirMultiplier;
    89. //      movement.z *= inAirMultiplier;
    90.     }
    91.        
    92.     movement += velocity;  
    93.     movement += Physics.gravity;
    94.     movement *= Time.deltaTime;
    95.    
    96.     // Actually move the character 
    97.     character.Move( movement );
    98.    
    99.     if ( character.isGrounded )
    100.     {
    101.         // Remove any persistent velocity after landing
    102.         velocity = Vector3.zero;
    103.     }
    104.    
    105. }
    106. function OnCollisionEnter (other : Collision)
    107. {
    108.     if (other.gameObject.tag == "Bounce")
    109.     {
    110.         rigidbody.AddForce(0, 10, 0);
    111.     }
    112. }
    Sorry if I had any grammar errors, not a native English speaker.
    As pointed by MDragon I didn't specify the problem. When the object(player) collides with the bouncy platform(tag: "Bounce") it doesn't bounce (addForce).
    Thank you.
     
    Last edited: Jan 11, 2014
  2. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    I wish I could help you, but I'm not sure what you need help with. The topic says "Problem with AddForce," but the info given only tells me you don't understand parts of the entire script so you can't find errors. What parts don't you understand, what are you trying to do, and any other info you think would be necessary would be helpful.

    Sorry for being so up-front about it, time to throw in a :D to make it less forward and demanding. :)
     
  3. TiredCoder

    TiredCoder

    Joined:
    Jan 11, 2014
    Posts:
    8
    Sorry. When the object(player) collides with the bouncy platform(tag: "Bounce") it doesn't bounce (addForce).
     
  4. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    A Character Controller doesn't have a Rigidbody? dafuq
     
  5. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    I guess there's no need to reverse engineer the entire code then (yay). The problem with AddForce is that it's adding a FORCE, not changing the velocity directly. You can either up that 10 value big time (or mess around with it however you'd like) to see results. For jumping, I tend to like just doing the following:
    rigidbody.velocity.y = new Vector3 (0,jumpSpeed,0); // Sets an absolute velocity, no matter the mass or current speed
    (the above is useful for jump pads or such. Made a similar version before just to add to the current velocity)

    But in your case, if you want to not have an absolute bounce speed and rather have it actually do a force (and thus take into consideration the mass and such), then use addForce. You can also do some other math to make the bounce depend on your falling speed- or even better yet, use the Physics materials for bouncing (since a built-in thing like that will save time and I'm pretty sure performance if you're just going to copy the performance done by the physics engine itself and its materials :) )

    Oh, and just in case it's something else, here's a debugging tip: Throw in print("Bounced... supposedly?"); within the if statement to see if it registers. (If that doesn't register, then check if the collision at all registers by the throwing that print or a Debug.log inside the CollisionEnter but outside of the if).

    Edit: Oh dang, Dave pointed something else out. The Character Controller is meant to be used WITHOUT forces. So, basically, either use the Character Controller or use rigidbodies and forces.

    I knew I should've actually read the script... at all :D

    Edit2: You can most likely still assign velocities and the like, if that velocity code was already built-in (read the Docs recently, said that the Character Controller is meant to be used without forces and the like, a useful and well-developed alternatives that still interacts with colliders basically).
     
    Last edited: Jan 11, 2014
  6. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    do something like
    Code (csharp):
    1. movement = new Vector3(0,10,0);
    or
    Code (csharp):
    1. movement.y = 10;
     
  7. TiredCoder

    TiredCoder

    Joined:
    Jan 11, 2014
    Posts:
    8
    Thank you. I didn't know a character controller couldn't have rigidbody and this was the problem.
     
  8. TiredCoder

    TiredCoder

    Joined:
    Jan 11, 2014
    Posts:
    8
    Okay so I searched in the forums and it seems that OnCollisionEnter() doesn't work with character controllers but if you put the code in the collider(bouncy platform) and make it a rigidbody you can use OnCollisionEnter().