RE-EDIT
Taking some much needed advice and simplifying.
All I’d like to do is see if I can have a character gameobject, which has a cube mesh character controller attached, can bounce or reflect it’s velocity when it hits a wall while moving instead of any kind of collider, just using collisionFlags.
I know of rigidbody physics and of using raycasting from sides while moving to detect sides but I don’t think they’ll work well with the controls on the gameobject.
My variables, function Update and calling the controller:
#pragma strict
private var moveDirection : Vector3 = Vector3.zero;
var gravity : float = 20.0;
var flightSpeed : float = 1.0;
var velocity : Vector3;
var curVelocity : Vector3;
var grounded : boolean = false;
var touchingWall : boolean = false;
var touchingCeiling : boolean = false;
static var chargeTime : float = 0;
static var chargeAmount = 0;
static var chargeLimit = 4;
static var isHolding : boolean = false;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
collisionFlags = controller.Move(moveDirection);
Within the function update I check for a raycast hit on the character gameobject. Then If raycast returns true set isHolding to true which will bring up directional arrow cubes. If collided with one send the game object in that direction. Only the code for sliding over the Upper Left box is shown for simplicity.
var hit : RaycastHit;
for (var touch in Input.touches)
{
if (touch.phase==TouchPhase.Stationary)
{
ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y, 0));
if (Physics.Raycast (ray, hit) hit.collider.gameObject.tag=="Player")
{
moveDirection = Vector3(0, 0, 0);
if(chargeAmount<3)
{
isHolding=true;
}else if (chargeAmount==3 touch.phase==TouchPhase.Ended)
{
isHolding=false;
}
}
}
if(touch.phase==TouchPhase.Moved)
{
ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x,touch.position.y, 0));
if (Physics.Raycast (ray,hit)&hit.collider.gameObject.tag=="UL")
{
if(isHolding==true)
{
chargeTime=0;
moveDirection = Vector3(1, 1.5, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= flightSpeed;
curVelocity = moveDirection;
if (touchingWall)
{
curVelocity = Vector3.Reflect (-curVelocity, Vector3.right);
}
}else if (isHolding==false)
{
moveDirection = Vector3(0, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
}
}
}
Then ensure that when the touch is ended that the directional arrows are set back to un-rendered.
if (touch.phase==TouchPhase.Ended)
{
ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x,touch.position.y, 0));
if (Physics.Raycast (ray,hit)&hit.collider.gameObject.tag=="Player")
{
chargeTime=0;
isHolding=false;
}
}
set gravity flags
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.Below) !=0;
touchingWall = (flags CollisionFlags.Sides) !=0;
touchingCeiling = (flags CollisionFlags.Above) !=0;
}
Essentially press and hold on the character brings up the arrows and starts the charge up that will rocket you further and faster the longer you hold. When you want slide your finger over the directional arrow you want and fly that way at that currently loaded flight speed.