Guest User

Untitled

a guest
Oct 15th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BOO 2.63 KB | None | 0 0
  1. import UnityEngine
  2.  
  3. class enderMovement (MonoBehaviour):
  4.     public jumpSpeed = 20.0 # move speed
  5.     public crawlSpeed = 1.0
  6.     public turnSpeed = 0.5 # turning speed (degrees)
  7.     public mouseSpeed = 10.0
  8.     public grabDistance = 1.0
  9.     public gravity = 10.0 # character gravity
  10.     public isGrounded as bool
  11.     public myCamera as Transform
  12.    
  13.     private myNormal as Vector3
  14.     private turnAngle = 0.0
  15.  
  16.     def Start():
  17.         myNormal = transform.up # starting character normal
  18.         rigidbody.freezeRotation = true # disable physics rotation
  19.  
  20.     def FixedUpdate():
  21.         ray as Ray
  22.         hit as RaycastHit
  23.         ray = Ray(transform.position, transform.up * -1)
  24.  
  25.         if Physics.Raycast(ray, hit):
  26.             isGrounded = hit.distance <= grabDistance
  27.             myNormal = Vector3.Lerp(myNormal, hit.normal, turnSpeed)
  28.             #myNormal.y = transform.localEulerAngles.y # This doesn't have a noticeable affect, but it seems like it would prevent the character from rotating to match a new normal?
  29.         else:
  30.             isGrounded = false
  31.  
  32.         if isGrounded and Input.GetButton("Down"):
  33.             rigidbody.drag = 2
  34.             # Rotate the camera vertically, independantly of parent, so that the character's down remains the same
  35.             # This is what you'd expect from typical FPS controls, while otherwise the vertical aiming acts like a space sim
  36.             myCamera.Rotate(Input.GetAxis("Mouse Y") * mouseSpeed * -1, 0, 0)
  37.            
  38.             # Hopefully, clamp the camera's roation to between -80 and 80
  39.             #myCamera.localEulerAngles.x = Mathf.Clamp(myCamera.localEulerAngles.x, -80, 80)
  40.            
  41.             # Rotate around Y axis, lerp to myNormal
  42.             turn = Input.GetAxis("Mouse X") * mouseSpeed
  43.             turnAngle = (turnAngle + turn) % 360 # This is mysterious. Probably not what I need for this, in the original script the character never left the surface it was adhering to
  44.             rot = Quaternion.FromToRotation(Vector3.up, myNormal)
  45.             rot *= Quaternion.Euler(0, turnAngle, 0)
  46.             transform.rotation = rot
  47.            
  48.             # Move around, basic WASD controls. Could use AddForce instead? AddForce acts strange.
  49.             transform.Translate(Input.GetAxis('Strafe') * crawlSpeed, 0,Input.GetAxis("Thrust") * crawlSpeed)
  50.             rigidbody.AddForce(-gravity * myNormal)
  51.         else:
  52.             rigidbody.drag = 0.1
  53.             # Lerp the camera to match the character. Would look better if the character lerped to match the camera, but seems tricky?
  54.             myCamera.rotation = Quaternion.Lerp(myCamera.rotation, transform.rotation, Time.deltaTime)
  55.            
  56.             # Rotate the character around its X and Y axis directly
  57.             transform.Rotate(Input.GetAxis("Mouse Y") * -1 * mouseSpeed, Input.GetAxis("Mouse X") * mouseSpeed, 0)
  58.            
  59.         if true:
  60.             if Input.GetButtonDown("Up"):
  61.                 rigidbody.AddForce((myCamera.forward * jumpSpeed))
Add Comment
Please, Sign In to add comment