Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UnityEngine
- class enderMovement (MonoBehaviour):
- public jumpSpeed = 20.0 # move speed
- public crawlSpeed = 1.0
- public turnSpeed = 0.5 # turning speed (degrees)
- public mouseSpeed = 10.0
- public grabDistance = 1.0
- public gravity = 10.0 # character gravity
- public isGrounded as bool
- public myCamera as Transform
- private myNormal as Vector3
- private turnAngle = 0.0
- def Start():
- myNormal = transform.up # starting character normal
- rigidbody.freezeRotation = true # disable physics rotation
- def FixedUpdate():
- ray as Ray
- hit as RaycastHit
- ray = Ray(transform.position, transform.up * -1)
- if Physics.Raycast(ray, hit):
- isGrounded = hit.distance <= grabDistance
- myNormal = Vector3.Lerp(myNormal, hit.normal, turnSpeed)
- #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?
- else:
- isGrounded = false
- if isGrounded and Input.GetButton("Down"):
- rigidbody.drag = 2
- # Rotate the camera vertically, independantly of parent, so that the character's down remains the same
- # This is what you'd expect from typical FPS controls, while otherwise the vertical aiming acts like a space sim
- myCamera.Rotate(Input.GetAxis("Mouse Y") * mouseSpeed * -1, 0, 0)
- # Hopefully, clamp the camera's roation to between -80 and 80
- #myCamera.localEulerAngles.x = Mathf.Clamp(myCamera.localEulerAngles.x, -80, 80)
- # Rotate around Y axis, lerp to myNormal
- turn = Input.GetAxis("Mouse X") * mouseSpeed
- 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
- rot = Quaternion.FromToRotation(Vector3.up, myNormal)
- rot *= Quaternion.Euler(0, turnAngle, 0)
- transform.rotation = rot
- # Move around, basic WASD controls. Could use AddForce instead? AddForce acts strange.
- transform.Translate(Input.GetAxis('Strafe') * crawlSpeed, 0,Input.GetAxis("Thrust") * crawlSpeed)
- rigidbody.AddForce(-gravity * myNormal)
- else:
- rigidbody.drag = 0.1
- # Lerp the camera to match the character. Would look better if the character lerped to match the camera, but seems tricky?
- myCamera.rotation = Quaternion.Lerp(myCamera.rotation, transform.rotation, Time.deltaTime)
- # Rotate the character around its X and Y axis directly
- transform.Rotate(Input.GetAxis("Mouse Y") * -1 * mouseSpeed, Input.GetAxis("Mouse X") * mouseSpeed, 0)
- if true:
- if Input.GetButtonDown("Up"):
- rigidbody.AddForce((myCamera.forward * jumpSpeed))
Add Comment
Please, Sign In to add comment