- Home /
putting jump in project#00
can anyone help me with my code i'm new to this. i'm trying to put a jump command in the code for project#00. i works with a few problems. the gravity is affecting the speed and when i hold space the sphere keeps jumping.
using UnityEngine; using System.Collections;
public class MoveAndJump : MonoBehaviour
{
public float jump = 8.0f;
public float gravity = 20.0f;
public float speed; public GUIText countText; public GUIText
winText; private int count;
private Vector3 moveDirection = Vector3.zero; private bool grounded = true; void start () { count = 0; SetCountText (); winText.text = ""; } void FixedUpdate () { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal,
moveDirection.y, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
if (grounded)
{
if(Input.GetButton ("Jump"))
moveDirection.y = jump;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
}
void OnTriggerEnter(Collider other) { if(other.gameObject.tag
== "PickUp") { other.gameObject.SetActive(false); count = count + 1; SetCountText (); } }
void SetCountText () { countText.text = "Count: " +
count.ToString(); if(count >=12) { winText.text = "YOU WIN"; } } }
the question editor glitched up... please reformat your code so that it is more readable.
Your answer
Follow this Question
Related Questions
Jumping By Set Amount Instead of By Speed 1 Answer
How do i code jumping for a 2d game in JS? 0 Answers
Character Code like Minecraft? 1 Answer
Jumping away from a wall 1 Answer
Changing Mesh of an object depending on Health Value. C# 2 Answers