- Home /
Ball movement script
Hi i am new to scripting and i am creating a 3D ball game. I have made a script that works but when hold the space bar instead of jumping once I fly it is something that needs to be fixed for me to make my game This is the script.
#pragma strict
function Start () {
}
function Update () {
rigidbody.AddForce(Input.GetAxis("Horizontal")*10,
Input.GetAxis("Jump")*25,
Input.GetAxis("Vertical")*10);
}
Answer by robertbu · Jul 14, 2013 at 06:50 PM
Try this:
function Update () {
var v3 = Vector3(Input.GetAxis("Horizontal")*10.0, 0.0, Input.GetAxis("Vertical")*10.0);
if (Input.GetButtonDown("Jump"))
v3 += Vector3.up * 25.0;
rigidbody.AddForce(v3);
}
Note you should explore the use of Time.deltaTime to scale your movements for different frame rates.
How can I change the Speed on your script ?
Oh, and can I use it for my game and make it public or must I show your name ? Thanks ! ^.^
Answer by __pierre__ · Mar 23, 2015 at 09:46 PM
Try:
bool Grounded(){ float dist = collider.bounds.extents.y; float max = 0.50f; // to give you some room return Physics.Raycast(transform.position, -Vector3.up, dist + max); }
also have a look at this: https://docs.unity3d.com/ScriptReference/RaycastHit-distance.html
Answer by Liraseth · Oct 08, 2019 at 07:39 AM
You can also try this.... Also make sure to give your ground a "Ground" tag through the Inspector. Hope this helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
public float speed;
public float jump = 20f;
private Rigidbody rb;
bool isGrounded = true;
float forward;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddRelativeForce(movement*speed);
}
private void Update()
{
bool player_jump = Input.GetButtonDown("Jump");
if (player_jump && isGrounded)
{
rb.AddForce(Vector3.up * jump);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
Your answer

Follow this Question
Related Questions
Need help with begginner script! 1 Answer
Speed Ball script 3 Answers
Ball wont go faster 2 Answers
How could I have a ball chooser menu? 1 Answer
this script is to reset the ball to its initial position after every kick in foorball game.. 1 Answer