Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by lachlan1234567 · Jul 14, 2013 at 06:46 AM · ball

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KidneySoup · Dec 18, 2014 at 01:54 PM 0
Share

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 ! ^.^

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
  }
  }

  



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges