- Home /
Jumping forward in sidescroller
Hi,I need help whit jumping forward in sidescroller.. Right now it only jumps up, but it's sidescroller so I need to make it jump forward and backward like in FPS controller! I am not good scripter so I would be happy if you help me whit it!
My script:
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
private var dead = false;
private var time = true;
private var accel = false;
private var slow = false;
var slowdown = 0.2;
var score = 1f;
var timescale = 1f;
var acc = 0.2;
var ReadyToFire = true;
var ReadyToSlow = true;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
var gravity = 20.0;
var JumpSpeed : float = 10;
function timecall(){
time = false;
yield WaitForSeconds (1);
timescale= timescale+1;
time = true;
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "dead")
{
Application.LoadLevel(2);
}
if(hit.gameObject.tag == "box")
{
//destroy the ammo box
Destroy(hit.gameObject);
score = score+1;
}
if(hit.gameObject.tag == "turbo")
{
//destroy the ammo box
Destroy(hit.gameObject);
speed = speed+6.5;
yield WaitForSeconds (2.75);
speed = speed-6.5;
}
if(hit.gameObject.tag == "Win")
{
Application.LoadLevel(3);
}
}
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var rigid : Rigidbody = GetComponent(Rigidbody);
if(grounded){
if (Input.GetButton("Jump")){
moveDirection.y = JumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
var right : Vector3 = transform.TransformDirection(Vector3.right);
var RotateSpeed : float = rotateSpeed * Input.GetAxis ("Horizontal");
controller.SimpleMove(right * RotateSpeed);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed;
controller.SimpleMove(forward * curSpeed);
}
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(-0.023, 3.8, -14.89);
gameObject.Find("Main Camera").transform.position = Vector3(-0.023, 3.8, -14.89);
dead = false;
}
}
@script RequireComponent(CharacterController)
Answer by senad · Mar 07, 2012 at 02:54 PM
Wow, that code is poorly formatted and hard to read. It will be much easier to get answers with more readable code in your question. ;)
In your update method you only set a value for the y-component of your move direction. Try setting also a constant value for the x-component. Once you reach the ground you can reset the x-movement again to zero.
I am not exactly sure how your game should behave, but this should help you find the general idea.
:)
Your answer
Follow this Question
Related Questions
movement doesnt stop when jump pad is tapped at the same time 0 Answers
Jump forward without moving? 1 Answer
3rd person controller, jump Error 1 Answer
Move forward and jump using touchpad 0 Answers
Jump. Script. 1 Answer