Search Unity

ETeeski 1.31 - stairs - colliders not working when running through them backwards

Discussion in 'Editor & General Support' started by benjwano, Mar 8, 2012.

  1. benjwano

    benjwano

    Joined:
    Mar 5, 2012
    Posts:
    6
    Hi, just going through ETeeski Tutorials, but have a small problem with the colliders at the top of the stairs.
    the colliders add force to the character so that he doesnt fly off the top of the stairs (ramp) when going up and down the stairs.

    Everything works and works as shown in the tutorial except for one aspect...
    when i move backwards through the colliders, they dont seem to work even though in the tutorial Eteeski shows the character going backwards and it works.

    Thanks in advance if anyone can help.

    if i have done this right, then the player movement script should be attached below (sorry, I am new to this forum)

    By the way Eteeski. Im still getting bullet holes blocked by box colliders (i think). it seems to be the same for these staircase colliders). will these also be sorted when the gun script is updated.

    thanks again Eteeski, great tutorials.

    Code (csharp):
    1. var currentGun : GameObject;
    2. var distToPickUpGun : float = 6;
    3. var throwGunUpForce : float = 100;
    4. var throwGunForwardForce : float = 300;
    5. var waitFrameForSwitchGuns : int = -1;
    6.  
    7. var walkAcceleration : float = 5;
    8. var walkAccelAirRacio : float = 0.1;
    9. var walkDeacceleration : float = 5;
    10. @HideInInspector
    11. var walkDeaccelerationVolx : float;
    12. @HideInInspector
    13. var walkDeaccelerationVolz : float;
    14.  
    15. var cameraObject : GameObject;
    16. var maxWalkSpeed : float = 20;
    17. @HideInInspector
    18. var horizontalMovement : Vector2;
    19.  
    20. var jumpVelocity : float = 20;
    21. @HideInInspector
    22. var grounded : boolean = false;
    23. var maxSlope : float = 60;
    24.  
    25. var crouchRacio : float = 0.3;
    26. var transitionToCrouchSec : float = 0.2;
    27. var crouchingVelocity : float;
    28. var currentCrouchRacio : float = 1;
    29. var originalLocalScaleY : float;
    30. var crouchLocalScaleY : float;
    31. var collisionDetectionSphere : GameObject;
    32.  
    33. @HideInInspector
    34. var waitToPickupAmmo : float = 0;
    35.  
    36.  
    37.  
    38. function Awake ()
    39. {
    40.     currentCrouchRacio = 1;
    41.     originalLocalScaleY = transform.localScale.y;
    42.     crouchLocalScaleY = transform.localScale.y * crouchRacio;
    43. }
    44.  
    45.  
    46. function LateUpdate ()
    47. {
    48.     waitFrameForSwitchGuns -= 1;
    49.     waitToPickupAmmo -= Time.deltaTime;
    50.  
    51.  
    52.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
    53.     if (Input.GetButton("Crouch"))
    54.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
    55.     if (Input.GetButton("Crouch") == false  collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
    56.         currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
    57.  
    58.    
    59.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    60.     if (horizontalMovement.magnitude > maxWalkSpeed)
    61.     {
    62.         horizontalMovement = horizontalMovement.normalized;
    63.         horizontalMovement *= maxWalkSpeed;    
    64.     }
    65.     rigidbody.velocity.x = horizontalMovement.x;
    66.     rigidbody.velocity.z = horizontalMovement.y;
    67.    
    68.  
    69.     if (grounded){
    70.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    71.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
    72.  
    73.    
    74.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    75.  
    76.    
    77.     if (grounded)
    78.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
    79.     else
    80.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
    81.  
    82.            
    83.     if (Input.GetButtonDown("Jump")  grounded)
    84.         rigidbody.AddForce(0,jumpVelocity,0);
    85. }
    86.  
    87.  
    88. function OnCollisionStay (collision : Collision)
    89. {
    90.     for (var contact : ContactPoint in collision.contacts)
    91.     {
    92.         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    93.             grounded = true;
    94.     }
    95. }
    96.  
    97.  
    98.  
    99. function OnCollisionExit ()
    100. {
    101.     grounded = false;
    102. }
    103.  
    104.  
    105. function OnTriggerStay (hitTrigger : Collider)
    106. {
    107.     if(hitTrigger.transform.tag == "StairGoingUp");
    108.         if(!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    109.             if(rigidbody.velocity.y > 0)
    110.                 rigidbody.velocity.y = 0;
    111.     if(hitTrigger.transform.tag == "StairGoingDown");
    112.         if(!Input.GetButton("Jump")  Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
    113.                 rigidbody.AddForce(0,-100,0);              
    114.  
    115.  
    116.     var current : GunScript = null;
    117.     if (currentGun)
    118.         current = currentGun.GetComponent(GunScript);
    119.     var ammo : AmmoPickUpScript = null;
    120.     var gun : GunScript = null;
    121.     if (hitTrigger.tag == "AmmoPickup"  waitToPickupAmmo <= 0)
    122.     {
    123.         ammo = hitTrigger.gameObject.GetComponent(AmmoPickUpScript);
    124.         if (current.currentExtraAmmo < current.maxExtraAmmo)
    125.         {
    126.             if (ammo.fromGun)
    127.             {
    128.                 gun = ammo.gun.GetComponent(GunScript);
    129.                 if (gun.currentExtraAmmo > 0  gun.ammoType == current.ammoType  ammo.gun != currentGun)
    130.                 {
    131.                 if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
    132.                     {
    133.                         gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
    134.                         current.currentExtraAmmo = current.maxExtraAmmo;
    135.                     }
    136.                     if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
    137.                     {
    138.                         current.currentExtraAmmo += gun.currentExtraAmmo;
    139.                         gun.currentExtraAmmo = 0;
    140.                     }
    141.                     if (ammo.pickupSound)
    142.                         ammo.gameObject.GetComponent(AudioSource).Play();
    143.                 }
    144.             }
    145.             if (!ammo.fromGun)
    146.             {
    147.                 if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
    148.                 {
    149.                     if(ammo.ammoAmount > 0  !ammo.unlimitedAmmo)
    150.                     {
    151.                         if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
    152.                         {
    153.                             ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
    154.                             current.currentExtraAmmo = current.maxExtraAmmo;
    155.                         }
    156.                         if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
    157.                         {
    158.                             current.currentExtraAmmo += ammo.ammoAmount;
    159.                             ammo.ammoAmount = 0;
    160.                         }
    161.                         if (ammo.pickupSound)
    162.                             ammo.gameObject.GetComponent(AudioSource).Play();  
    163.                     }
    164.                     if (ammo.unlimitedAmmo)
    165.                     {
    166.                         current.currentExtraAmmo = current.maxExtraAmmo;
    167.                         if (ammo.pickupSound)
    168.                             ammo.gameObject.GetComponent(AudioSource).Play();
    169.                     }
    170.                 }
    171.             }
    172.         }
    173.     }
    174. }
     
  2. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    your script looks fine.
    maybe it's the positioning of the trigger cubes at the top of the stairs.
    first, make sure you have to movement set to local:
    View attachment 31899

    then make sure the stair going down one is pointing in the direction down the stairs:
    View attachment 31900

    then make sure the stair going up one is pointing in the direction going up the stairs:
    View attachment 31901

    as for the bullets, yeah, I've been getting the same thing. I will update that glitch when i also update the gunscript.
     
  3. benjwano

    benjwano

    Joined:
    Mar 5, 2012
    Posts:
    6
    Hi Eteeski, thanks for the reply.

    I now have it working.
    just had to get back to basics and start again step by step.

    for anyone else having problems (some issues I may of had), it may help considering a few things such as...
    1. I had made my staircase quite small (more in line with the scale of the player)
    2. At one point I also set my walk speeds quite high for testing.
    Because of this, my player was running through the stair(down) collider to fast and the collider was also to small
    for the addforce code to work / have enough effect on the player for me to notice it was working.

    By the way Eteeski, when I run and stop halfway up the stairs. I slide back down again. I assum this is because the player is really on a ramp. Is there an easy way to solve this, or will it be covered later on in the tuts.

    thanks again for the help. You seem to have a lot on. dont know how you have the time to fit it all in :)


    edit: forgot to add, I ended up making my colliders the full lengh of the stairs and angled them the same as the stairs. It seemd to have a smoother effect on my stairs model. Can you forsee any issues with this.
     
    Last edited: Mar 10, 2012