- Home /
Question by
appmonster · Aug 15, 2011 at 04:32 PM ·
movementcharactercontrollerplatformerbce0005
Moving platform and character controller
Hello,
I am having trouble trying to understand the scripting for moving my character controller along with a moving platform. I would like some help in trying to figure out how to get my character to move along with my moving/rotating platforms. I also keep getting the following errors:
Assets/Scripts/MoveAround.js(93,22): BCE0005: Unknown identifier: 'myCharacterController'. Assets/Scripts/MoveAround.js(93,50): BCE0005: Unknown identifier: 'calculatedMovement'.
Please find attached my script for my character:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;
private var dead = false;
// Moving platform support
private var activePlatform : Transform;
private var activeLocalPlatformPoint : Vector3;
private var activeGlobalPlatformPoint : Vector3;
private var lastPlatformVelocity : Vector3;
// If you want to support moving platform rotation as well:
private var activeLocalPlatformRotation : Quaternion;
private var activeGlobalPlatformRotation : Quaternion;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
// Make sure we are really standing on a straight platform
// Not on the underside of one and not falling down from it either!
if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.5) {
activePlatform = hit.collider.transform;
}
if(hit.gameObject.tag == "fallout")
{
dead = true;
}
}
function OnTriggerEnter( hit : Collider)
{
if(hit.gameObject.tag == "fireBall")
{
dead = true;
Destroy(hit.gameObject);
}
}
function Update() {
if (Input.GetButton ("Jump")) { // of set it to jumpSpeed if Jump pressed
moveDirection.y = jumpSpeed;
}
}
// Apply gravity anyway
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
// Moving platform support
if (activePlatform != null) {
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
if (moveDistance != Vector3.zero)
controller.Move(moveDistance);
lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
// If you want to support moving platform rotation as well:
var newGlobalPlatformRotation = activePlatform.rotation * activeLocalPlatformRotation;
var rotationDiff = newGlobalPlatformRotation * Quaternion.Inverse(activeGlobalPlatformRotation);
// Prevent rotation of the local up vector
rotationDiff = Quaternion.FromToRotation(rotationDiff * transform.up, transform.up) * rotationDiff;
transform.rotation = rotationDiff * transform.rotation;
}
else {
lastPlatformVelocity = Vector3.zero;
}
activePlatform = null;
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Calculate forth/back velocity vector
var forward : Vector3 = transform.forward * speed * Input.GetAxis("Vertical");
forward.y = moveDirection.y; // keep current vertical velocity
moveDirection = forward; // but update horizontal velocity
if (controller.isGrounded) {
moveDirection.y = 0; // zero vertical velocity if already grounded
collisionFlags = myCharacterController.Move (calculatedMovement);
// Moving platforms support
if (activePlatform != null) {
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
// If you want to support moving platform rotation as well:
activeGlobalPlatformRotation = transform.rotation;
activeLocalPlatformRotation = Quaternion.Inverse(activePlatform.rotation) * transform.rotation;
}
}
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(-28,545.8922,-25.69122);
gameObject.Find("Main Camera").transform.position = Vector3(-17.58673,603.5624,-86.56421);
dead = false;
}
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterController)
Comment