- Home /
I want a flying / floating Charcter
Hello folks! I am currently working on a 2D sidescroller game for 2 players. Player 1 plays the jump 'n run part and has to walk from left to right. Player 2 controls a platform on which player 1 can jump so he doesn't fall. That's why player 2 needs to control the platform freely, like up, down, left, right (zero-gravity or something similar). I just can't figure out how to write the code for that. Currently it looks like this:
var normalSpeed:float=6.0;
private var speed:float=normalSpeed;
var runSpeed:float=12.0;
private var jumpSpeed:float=speed*1.7;
var gravity:float=20.0;
private var walkTime:int=0;
private var moveDirection:Vector3=Vector3.zero;
static var grounded:boolean=false;
private var controller:CharacterController;
private var flags:CollisionFlags;
private var tr:int=90;
function Start(){
animation.wrapMode=WrapMode.Loop;
animation["run"].layer=-1;
animation["walk"].layer=-1;
animation["idle"].layer=-1;
animation.SyncLayer(-1);
animation["jump"].layer=10;
animation["jump"].wrapMode=WrapMode.Once;
animation.SyncLayer(10);
animation.Stop();
animation.Play("idle");
}
function FixedUpdate(){
if(grounded){
moveDirection=new Vector3(Input.GetAxis("HorizontalP1"),0,0); //new
//moveDirection = transform.TransformDirection(moveDirection); //new
moveDirection*=speed;
if(Input.GetButton("JumpP1")){
moveDirection.y=jumpSpeed;
animation.CrossFade("jump");
}
}else{
moveDirection=new Vector3(Input.GetAxis("HorizontalP1"),moveDirection.y/speed,0);
moveDirection*=speed;
if (Input.GetButton ("Jump")) { //new
moveDirection.y = jumpSpeed; //new
} }
moveDirection.y-=gravity*Time.deltaTime;
controller=GetComponent(CharacterController);
flags=controller.Move(moveDirection*Time.deltaTime);
grounded=(flags & CollisionFlags.CollidedBelow) !=0;
if(flags && CollisionFlags.CollidedBelow){
grounded=true;
}else{
grounded=false;
}
//if(moveDirection.x>0){
//tr=90;
//}else if(moveDirection.x<0){
//tr=270;
//}
//transform.eulerAngles.y-=(transform.eulerAngles.y-tr)/5;
//tr is the target rotation
//var currentRotation=transform.eulerAngles.y;
//var rotationDifference=currentRotation-tr;
//var amountToRotate=rotationDifference/5;
//transform.eulerAngles.y -= amountToRotate;
if(Input.GetAxis("HorizontalP1")>.2 || Input.GetAxis("HorizontalP1")<-.2){
if(walkTime>40){
animation.CrossFade("run");
speed=runSpeed;
}else{
walkTime++;
animation.CrossFade("walk");
speed=normalSpeed;
}
jumpSpeed=speed*1.7;
}else{
walkTime=0;
animation.CrossFade("idle");
}
}
@script RequireComponent(CharacterController)
I hope you can help me! greetings, T.
couldn't you do this by setting the "is$$anonymous$$inematic" variable in the RigidBody options?
though this would require you to maybe control the increments of the movement.
Answer by timo · May 26, 2012 at 08:00 PM
thanks, I found a solution!
can you tell us what that solution is? personally, I would like to know