Since the site had a hiccup and erased everything I just entered except the tags and title after freezing up, I am going to make this one short and sweet:
I have been trying to learn 3D Game Development with Unity3D Android. To do this, I have spent a week going through youtube video tutorials by Pierce Thompson. This guy cut his first set of videos off then his 2nd set was even more all over the place. He somehow fixed his code to make his character jump by first hitting the “Stop Recording” button on his camera then the “Resume Recording” button…then didn’t post his finished code or changes. I’m no master coder but that doesn’t seem like it would work.
I have fixed a bunch of his broken code and added a few extras in on my own by trial and error. However, I just can’t figure out how to do two things:
-
Create a button instead of a joystick
-
Make the character jump
The closest I have gotten is using the tapCount of a joystick to increase the lateral movement speed of the character’s current movement direction by adding jumpSpeed. I have not successfully gotten the character off the ground…unless I run off the side of the terrain…at which time there is no more ground so I just keep falling LOL.
I like to figure things out on my own but with this, I have done about 150 google searches, unity community searches, and tutorial reviews and have not found anything on this subject.
Please help me do the following two things:
-
Change my JumpPad from joystick to button
-
Get my character to jump
The following is in JavaScript:
//Says this script is required to run the game
@script RequireComponent(CharacterController)
//Default access variables
var LeftJoy : Joystick;
var RightJoy : Joystick;
var JumpPad : Joystick;
//For rotation of camera
var cameraPivot : Transform;
//Movement
var forwardSpeed : float = 4f;
var backwardSpeed : float = 2f;
var sidestepSpeed : float = 2f;
var jumpSpeed : float = 8f;
var inAirMultiplier : float = 0.25f;
var rotationSpeed : Vector2 = Vector2(50, 25);
var tiltPositiveYAxis = 0.6;
var tiltNegativeYAxis = 0.4;
var tiltXAxisMinimum = 0.1;
var Turtle : GameObject;
//Private variable declarations
private var thisTransform : Transform;
private var character : CharacterController;
private var cameraVelocity : Vector3;
private var velocity : Vector3;
private var canJump = true;
//Called at start of game
function Start ()
{
//Defines components to speed up in game loading time
thisTransform = GetComponent(Transform);
character = GetComponent(CharacterController);
Turtle = GameObject.FindGameObjectWithTag("Turtle");
LeftJoy = GameObject.FindGameObjectWithTag("LeftJoy").GetComponent(Joystick);
RightJoy = GameObject.FindGameObjectWithTag("RightJoy").GetComponent(Joystick);
JumpPad = GameObject.FindGameObjectWithTag("JumpPad").GetComponent(Joystick);
//Move player to spawn point if it exists
var spawn = GameObject.Find("PlayerSpawn");
if(spawn)
{
thisTransform.position = spawn.transform.position;
}
}
//Function for when game ends
function onEndGame()
{
LeftJoy.Disable();
RightJoy.Disable();
JumpPad.Disable();
this.enabled = false;
}
//Called every frame
function Update ()
{
var movement = thisTransform.TransformDirection(Vector3(LeftJoy.position.x, 0, LeftJoy.position.y));
//Normalizes frame rate like Delta Time. Equalizes frame rate from code to device allowability
movement.Normalize();
var absJoyPos = Vector2(Mathf.Abs(LeftJoy.position.x), Mathf.Abs(LeftJoy.position.y));
if(absJoyPos.y > absJoyPos.x)
{
if(LeftJoy.position.y > 0)
{
movement *= forwardSpeed * absJoyPos.y;
Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.y;
Turtle.animation.CrossFade("Turtle_Walk");
}
else
{
movement *= backwardSpeed * absJoyPos.y;
Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.y;
Turtle.animation.CrossFade("Turtle_Walk");
}
}
else if(absJoyPos.y == absJoyPos.x)
{
Turtle.animation["Turtle_Idle"].speed = 0.75f;
Turtle.animation.CrossFade("Turtle_Idle");
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.x;
Turtle.animation.CrossFade("Turtle_Walk");
}
if(character.isGrounded)
{
var jump = false;
if(!JumpPad.IsFingerDown())
{
canJump = true;
}
if(JumpPad.IsFingerDown())
{
print("jump button pressed");
//Jump syntax goes here
}
}
else
{
velocity.y += Physics.gravity.y * Time.deltaTime;
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move(movement);
if(character.isGrounded)
{
var camRotation = Vector2.zero;
if(RightJoy)
{
camRotation = RightJoy.position;
}
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
thisTransform.Rotate(0, camRotation.x, 0, Space.World);
cameraPivot.Rotate(-camRotation.y, 0, 0);
}
}