Hi everyone! I'm working on a very simple game to get myself familiar with Unity iPhone and I've got almost all the major parts working great but I was wondering if anyone can help me out with one little problem. I've got a player character that's setup to move forward, backward, and sideways by tilting the iPhone and he jumps the moment he's on the ground, over and over. This is actually what I want for the gameplay. My only trick now is getting my player character to correctly move through platforms so he can jump on top of them but have no trouble passing right through them from underneath. I'm using flat polygon meshes for collision so he's able to make it through but he seems to collide with them from the sides and their interiors making movement a bit clunky when he's jumping up through groups of small platforms. Here's my code for the character controller, kind of a stripped down hack job of the Player Relative Control script included with Unity iPhone:
@script RequireComponent( CharacterController )
// This script must be attached to a GameObject that has a CharacterController
var forwardSpeed : float = 4;
var backwardSpeed : float = 1;
var sidestepSpeed : float = 1;
var jumpSpeed : float = 8;
private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3; // Used for continuing momentum while in air
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function Update()
{
var dir : Vector3 = Vector3.zero;
dir.x = -iPhoneInput.acceleration.y;
dir.z = iPhoneInput.acceleration.x;
var movement = thisTransform.TransformDirection( Vector3( dir.x, 0, dir.z ) );
// We only want horizontal movement
movement.y = 0;
movement.Normalize();
// Apply movement from move joystick
var absJoyPos = Vector2( Mathf.Abs( dir.x ), Mathf.Abs( dir.z ) );
if ( absJoyPos.y > absJoyPos.x )
{
if ( dir.z > 0 )
movement *= forwardSpeed * absJoyPos.y;
else
{
movement *= backwardSpeed * absJoyPos.y;
}
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
}
// Check for jump
if ( character.isGrounded )
{
// Apply the current movement to launch velocity
// velocity = character.velocity;
velocity.y = jumpSpeed;
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
}
Thanks looking and any ideas someone might have! I am LOVING this Unity stuff SO MUCH! Seriously best development community I have EVER SEEN!
Oh and any ideas on simplifying my code for tilting to move and adjusting the starting angle the iPhone is held at would be AMAZING but no worries on that, I'll find ways eventually. XD