Search Unity

walking on walls script.

Discussion in 'Scripting' started by leenffc, Feb 20, 2014.

  1. leenffc

    leenffc

    Joined:
    Feb 20, 2014
    Posts:
    11
    Hi,

    I am looking at making a game where the players can walk on walls whilst still being able do stuff such as jump and shoot.

    could anybody help me with the script for this as im still a begginner in unity.

    Thanks
     
  2. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
  3. leenffc

    leenffc

    Joined:
    Feb 20, 2014
    Posts:
    11
    i have looked at this thread and applied it to my game but my player just seems to glitch and fall through the walls in my game, any fix for this?
     
  4. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Maybe this helps:
    Code (csharp):
    1.  
    2. /*
    3. Create a sphere object and scale every axis up a bit (say by 10-15m)
    4. Create a cube and attach this script to it
    5. Script requires a sphere collider as an argument so select the sphere game object.
    6. Use arrow keys to fly around!
    7. */
    8.  
    9. var rotationSpeed = 120.0;
    10.  
    11. var translationSpeed = 10.0;
    12.  
    13. var height = 2.0;          //height from ground level
    14.  
    15. private var centre : Transform;            //transform for planet
    16.  
    17. private var radius : float;               //calculated radius from collider
    18.  
    19. var planet : SphereCollider ;         //collider for planet
    20.  
    21.  
    22.  
    23.  
    24.  
    25. function Start ()
    26.  
    27.  
    28.  
    29. {
    30.  
    31.       //consider scale applied to planet transform (assuming uniform, just pick one)
    32.  
    33.       radius = planet.radius * planet.transform.localScale.y;
    34.  
    35.       centre = planet.transform;
    36.  
    37.       //starting position at north pole
    38.  
    39.       transform.position = centre.position + Vector3(0,radius+height,0);
    40.  
    41. }
    42.  
    43.  
    44.  
    45. function Update ()
    46.  
    47.  
    48.  
    49. {
    50.  
    51.       //translate based on input      
    52.  
    53.       var inputMag  = Input.GetAxis("Vertical")*translationSpeed*Time.deltaTime;
    54.  
    55.       transform.position += transform.forward * inputMag;
    56.  
    57.         //snap position to radius + height (could also use raycasts)
    58.  
    59.       targetPosition = transform.position - centre.position;
    60.  
    61.       var ratio = (radius + height) / targetPosition.magnitude;
    62.  
    63.       targetPosition.Scale(Vector3(ratio, ratio, ratio) );
    64.  
    65.       transform.position = targetPosition + centre.position;
    66.  
    67.       //calculate planet surface normal                      
    68.  
    69.       surfaceNormal = transform.position - centre.position;
    70.  
    71.       surfaceNormal.Normalize();
    72.  
    73.       //GameObject's heading
    74.  
    75.       var headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed;
    76.  
    77.       headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
    78.  
    79.       //align with surface normal
    80.  
    81.       transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) * transform.rotation;
    82.  
    83.       //apply heading rotation
    84.  
    85.       transform.rotation = headingDelta * transform.rotation;
    86.  
    87.    }
    88.  
    Please show me your result, Thanks
     
  5. leenffc

    leenffc

    Joined:
    Feb 20, 2014
    Posts:
    11
    it works and it on the same lines of what i want. i want to create a first person shooter game such as counter strike etc with the player having the ability to go anywhere on the map by sticking to walls/ceilings and treating them as floors.
     
  6. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    I was working with that script too (still am) There was a bit of an error i was getting, and the fix had been moving the collider up in the inspector (so it was underneath the transform on the empty object) not sure why... this fixed it... and not sure it'll fix your problem but it really helped with mine.