- Home /
Question by
potu1304 · Sep 07, 2012 at 08:31 PM ·
c#movementjumpcharacter controller
Extend simple movement script (C#)
Hello!
I have a simple movement script: But I would like to be bale to jump in directions and be able to control the character in the air, if you knwo what I mean. Is there a way to extend my script like that?
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float f_speed;
public float f_jumpSpeed;
public float f_gravity;
private Vector3 v3_moveDirection = Vector3.zero;
private CharacterController controller;
public float jumpInterval = 5;
private float nextJump = 0;
void Awake ()
{
controller = GetComponent<CharacterController>();
}
void Update ()
{
if (controller.isGrounded)
{
v3_moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
v3_moveDirection *= f_speed;
if (Input.GetButton("Jump")&& Time.time > nextJump)
{
v3_moveDirection.y = f_jumpSpeed;
nextJump = Time.time + jumpInterval;
}
}
v3_moveDirection.y -= f_gravity * Time.deltaTime;
controller.Move(v3_moveDirection * Time.deltaTime);
}
}
Comment
Answer by Mander · Sep 07, 2012 at 09:26 PM
ofcs is possible try this (i just added the else if) else if my char is not on the ground move on x depending of input * speed
if (controller.isGrounded)
{
v3_moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
v3_moveDirection *= f_speed;
if (Input.GetButton("Jump")&& Time.time > nextJump)
{
v3_moveDirection.y = f_jumpSpeed;
nextJump = Time.time + jumpInterval;
}
}else if(!controller.isGrounded){
v3_moveDirection.x = Input.GetAxis("Horizontal") * f_speed;
}