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
0
Question by lachlan1234567 · Jul 07, 2013 at 11:22 AM · ballmovment

Movment Script

I have just made a Movement script For a ball game but i can only go forward in one movement so when I move the camera The ball goes sideways. Also I want my ball to be able to jump but it flys when you hold space? how can i fix that in 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 · Show 4
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 gamerant · Jul 07, 2013 at 06:58 PM 0
Share

You have no variable declarations or if statements so your script has no logic that needs to be carried out.First try declaring variables and using them in your code.It is the convention and allows for easier script altering for yourself and anyone else if needed. Second you need to put your Input statements into an if statement.Try something like this:

 If(Input.GetButtonDown("Jump"))
 {
  rigidbody.AddForce(0,jumpVel,0);
 }

What ever the logic you want to be carried out will be done inside the if statement.

Also try watching EteeskiTutorials FPS1.5 if you can't get this yourself as he goes over jumping and how it works very well.

avatar image Daniel G · Jul 07, 2013 at 07:09 PM 0
Share

Yes gamerant is correct, you need to have if statements, function Update () happens at least once per frame. SO every frame that code is run, this means the force is added to the ball every frame of every second. Thats why you need logic like if statements to make that one line of code only happen once PER spacebar press.

avatar image lachlan1234567 · Jul 08, 2013 at 12:47 AM 0
Share

How would i do that in my script??

avatar image gamerant · Jul 08, 2013 at 10:33 AM 0
Share

I assume you mean how to add logic to a script. Well if you are new to scripting I suggest and would strongly urge you to look at tutorial videos here on unity it self http://unity3d.com/learn/tutorials/modules or simply go on youtube.There are ton of great tutorials there.

But to answer your question,take a look at the example I gave you above.It says that if you press the jump button then a force is added to your rigid body along the y axis.This simply means that only when the button is pressed does the object in the game jump(or move up).

Inside the if brackets if() is where you put what you want your game to do.So in this case you are saying that you want the object to jump if the jump button is pressed down.Then inside {} these brackets is where your game logic goes.It's what you tell the program to do when the button is pressed.In this case you are telling it to add force to the rigid body.The brackets after AddForce tells your game which direction to add the force in.`(0,jumpVel,0)` simply says that there is no force added to the object along the x and the z axis since they are 0.But a force is added to the object along the y axis. JumpVel is the declare variable with which you can control how much force is added along the y. If it's small then a small amount of force is added. To declare this you would simply put var jumpVel : float; outside all of your functions.What this does is add the declared variable to the in game inspector which allows you to tweak the value of the force without having to go back to your script all the time. The float means that your variable can have decimal points and gives a much greater accuracy and control for when you try to find a nicely working value for jumping.

Oh and quickly: GetButtonDown means that when the button is pressed you will jump once.If you have GetButton your object would move up along the y axis as long as you have the jump button pressed.

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simulating Marble Blast Gold: A script to make a character as a marble roll forward, mouse turns screen? 2 Answers

Ball not going to directions 0 Answers

Player flies PROBLEM 1 Answer

Platform falling script 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