Hello. I’ve started watching a tutorial on youtube on how to make a 2d platformer using unity3d.
http://www.youtube.com/watch?v=EW0phq6xoJk This is the link to the video.
I’ve started port the js code provided in the description to boo, to practice and learn a bit about js, since I come from a python background.
Here’s the original code: http://db.tt/6Z1uSVUd
And here’s my port to boo:
import UnityEngine
canControl = true
spawnPoint = Transform()
class PlatformerControllerMovement():
walkSpeed = 3.0
runSpeed = 10.0
inAirControlAcceleration = 1.0
gravity = 60.0
maxFallSpeed = 20.0
speedSmoothing = 5.0
rotationSmoothing = 10.0
direction = Vector3.zero
[System.NonSerialized]
verticalSpeed = 0.0
[System.NonSerialized]
speed = 0.0
[System.NonSerialized]
isMoving = false
[System.NonSerialized]
collisionFlags as CollisionFlags
[System.NonSerialized]
velocity = Vector3
[System.NonSerialized]
inAirVelocity = Vector3.zero
[System.NonSerialized]
hangTime = 0.0
movement = PlatformControllerMovement()
class PlatformerControllerJumping():
enabled = true
height = 1.0
extraHeight = 4.1
[System.NonSerialized]
repeatTime = 0.05
[System.NonSerialized]
timeout = 0.15
[System.NonSerialized]
jumping = false
[System.NonSerialized]
reachedApex = false
[System.NonSerialized]
lastButtonTime = -10.0
[System.NonSerialized]
lastTime = -1.0
[System.NonSerialized]
var lastStartHeight = 0.0
jump = PlatformerControllerJumping()
controller = CharacterController()
activePlatform = Transform()
activeLocalPlatformPoint = Vector3()
activeGlobalPlatformPoint = Vector3()
lastPlatformVelocity = Vector3()
areEmittersOn = false
def Awake():
movement.direction = transform.TransformDirection(Vector3.forward)
controller = GetComponent(CharacterController)
Spawn()
def Spawn():
movement.verticalSpeed = 0.0
movement.speed = 0.0
transform.position = spawnPoint.position
def OnDeath():
Spawn()
def UpdateSmoothedMovementDirection():
h = Input.GetAxisRaw("Horizontal")
if canControl == false:
h = 0.0
movement.isMoving = Mathf.Abs(h)>0.1
if movement.isMoving == true:
movement.direction = Vector3(h, 0,0)
if controller.isGrounded == true:
curSmooth = movement.speedSmoothing * Time.deltaTime
targetSpeed = Mathf.Min(Mathf.Abs(h), 1.0)
if Input.GetButton("Fire2") == true and canControl == true:
targetSpeed *= movement.runSpeed
else:
targetSpeed *= movement.walkSpeed
movement.speed = Mathf.Lerp(movement.speed, targetSpeed, curSmooth)
movement.hangTime = 0.0
else:
movement.hangTime += Time.deltaTime
if movement.isMoving = true:
movement.inAirVelocity += Vector3(Mathf.Sign(h), 0,0) * Time.deltaTime * movement.inAirControlAcceleration
def FixedUpdate():
transform.position.z = 0
def ApplyJumping():
if jump.lastTime + jump.repeatTime > Time.time:
return
if controller.isGrounded == true:
if jump.enabled and Time.time < jump.lastButtonTime + jump.timeout:
movement.verticalSpeed = CalculateJumpVerticalSpeed(jump.height)
movement.inAirVelocity = lastPlatformVelocity
SendMessage("DidJump", SendMessageOptions.DontRequireReceiver)
def ApplyGravity():
jumpButton = Input.GetButton("Jump")
if canControl == false:
jumpButton = false
if jump.jumping == true and jump.reachedApex == false and movement.verticalSpeed <= 0.0
jump.reachedApex = true
SendMessage("DidJumpReacheApex", SendMessageOptions.DontRequireReceiver)
extraPowerJump = jump.jumping == true and movement.verticalSpeed > 0.0 and jumpButton == true and transform.position.y < jump.lastStartHeight + jump.extraHeight and IsTouchingCeiling() == false
if extraPowerJump == true:
return
elif controller.isGrounded == true:
movement.verticalSpeed = -movement.gravity * Time.deltaTime
else:
movement.verticalSpeed -= movement.gravity * Time.deltaTime
movement.verticalSpeed = Mathf.Max(movement.verticalSpeed, -movement.maxFallSpeed)
def CalculateJumpVerticalSpeed(targetJumpHeight):
return Mathf.Sqrt(2.0 * targetJumpHeight * movement.gravity)
def DidJump()
jump.jumping = true
jump.reachedAped = false
jump.lastTime = Time.time
jump.lastStartHeight = transform.position.y
jump.lastButtonTime = -10
def UpdateEffects():
wereEmittersOn = areEmittersOn
areEmittersOn = jump.jumping and movement.verticalSpeed > 0.0
if wereEmittersOn != areEmittersOn:
for emitter in GetComponentsInChildren(ParticleEmitter):
emitter.emit = areEmittersOn
def Update():
if Input.GetButtonDown("Jump") and canControl == true:
jump.lastButtonTime = Time.time
UpdateSmoothedMovementDirection()
ApplyGravity()
ApplyJumping()
if activePlatform != null:
newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint)
moveDiscance = (newGlobalPlatformPoint - activeGlobalPlatformPoint)
transform.position = transform.position + moveDistance
lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime
else:
lastPlatformVelocity = Vector3.zero
activePlatform = null
lastPosition = transform.position
currentMovementOffset = movement.direction * movement.speed + Vector3(0, movement.verticalSpeed, 0) + movement.inAirVelocity
currentMovementOffset *=Time.deltaTime
movement.collisionFlags = controller.Move(currentMovementOffset)
movement.velocity = (transform.position - lastPosition) / Time.deltaTime
if activePlaform != null:
activeGlobalPlaftormPoint = transform.position
activeLocalPlaformPoint = activePlatform.InverseTransformPoint(transform.position)
if controller.isGrounded == true:
movement.inAirVelocity = Vector3.zero
if jump.jumping == true:
jump.jumping = false
SendMessage("DidLand", SendMessageOptions.DontRequireReceiver)
jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity
if jumpMoveDirection.sqrMagniture > 0.01:
movement.direction = jumpMoveDirection.normalized
UpdateEffects()
def OnControllerColliderHit(hit = ControllerColliderHit):
if hit.moveDirection.y > 0.01:
return
if hit.moveDirection.y < -0.9 and hit.normal.y > 0.9:
activePlatform = hit.collider.transform
def GetSpeed():
return movement.speed
def GetVelocity():
return movement.velocity
def IsMoving():
return movement.isMoving
def IsJumping():
jump.jumping
def IsTouchingCeiling():
return movement.collisionFlags != 0 and CollisionFlags.CollidedAbove != 0
def GetDirection():
return movement.direction
def GetHangTime():
movement.hangTime
def Reset():
gameObject.tag = "Player"
def SetControllable(controllable):
canControl = controllable
[RequireComponent(CharacterController)]
[AddComponentMenu("2D Platformer/Platformer Controller")]
The error I get when trying to run the game:
Assets/Scripts/PlatformerController.boo(7,1):
BCE0044: expecting “EOF”, found class.
I can’t seem to realize what does the error mean.
This is the first time I’m working with Unity3d and boo, I’d really appreciate if anyone could help me.