Search Unity

How do I make a ball jump and reset once it falls off game?

Discussion in 'Scripting' started by testingscript64, Mar 7, 2015.

  1. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
    Hi all

    I'm new to Unity and I'm using the example project I made with the roll-a-ball project tutorial videos as my base for a game. I want the ball to jump (when the player presses the space button) once until it hits the ground. I also want the ball to reset its position if it falls a certain distance.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         float moveHorizontal = Input.GetAxis ("Horizontal");
    12.         //float moveVertical = Input.GetAxis ("Vertical");
    13.  
    14.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
    15.  
    16.         rigidbody.AddForce (movement * speed * Time.deltaTime);
    17.     }
    18. }
     
    Last edited: Mar 7, 2015
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    What do you mean by vertical position? If you mean the Z axis, well just don't use it. If you mean the Y axis, then I am wondering how you think that would work, as jumping will change the Y position by default.
     
  3. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
    I'm not using it, it's just that whenever the ball runs over an obstacle like a bump, it would cause it to ruin the Z position of the ball in the 2D level (this is a 3D project), which then makes it prone to falling off the stage. I'm also new to programming, so I don't know how to make the ball jump or set the Z axis to 0.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you view the rigidbody in the editor, it has constraints. You can force it to not move or rotate in any of the axes. In code you can also set that constraint as well, by logical-OR-ing the required bitfields together. Look up scripting docs on Rigidbodies.
     
  5. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
    Didn't know that, thanks for telling me.
     
  6. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
  7. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Resetting the z position to 0 is VERY easy. You can do this:

    Code (csharp):
    1. public void ResetPosition ()
    2. {
    3.     transform.position = Vector3.zero; //Resets entire transform position, including X axis, Y axis, and Z axis
    4.     //OR
    5.     transform.position.z = 0.0f; //Resets Z axis only, so if you don't want the other axis to reset, use this
    6. }
    7.  
    For the ball resetting after a certain distance, make sure that the ball's position is at 0 by default, then do this:

    Code (csharp):
    1. if (transform.position.z < -16) //The -16 can be any negative vale of your choice
    2. {
    3.     ResetPosition ();
    4.     //OR
    5.     Application.LoadLevel ("Test Level" /* the name of your scene between those double quotes*/);
    6.     //The above line will reload the level, so if you don't want that to happen, DO NOT use that method.
    7. }
    8.  
    EDIT:
    Is your game a side scroller? If it use, make sure to change the
    Code (csharp):
    1. transform.position.z;
    to
    Code (csharp):
    1. transform.position.y;
    Also, see this!
    http://docs.unity3d.com/ScriptReference/Transform.html
     
    Last edited: Mar 7, 2015
  8. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
    I can't run that script because "transform.position.y = 0.0f;" seems to be causing an error.

    Assets/scripts/PlayerController.cs(12,27): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You can do transform.position.y assignments in JavaScript, but not in C#. In C# you must do it a little differently, by assigning the entire Vector3 object, like so:

    transform.position = new Vector3( transform.position.x, 0, transform.position.z);

    It has to do with how the .position "variable" is really a C# property and needs to be set in its entirety, as it is a value property (struct), not a class.
     
  10. testingscript64

    testingscript64

    Joined:
    Mar 6, 2015
    Posts:
    8
    I changed the game, now it has a paddle. I can control the paddle, but it goes too fast. Any way to decrease it's speed?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     public void ResetPosition ()
    9.     {
    10.         //transform.position.y = 0.5f;
    11.         //transform.position.z = 0.5f;
    12.         //transform.position.x = 0.5f;
    13.     }
    14.  
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis ("Horizontal");
    19.         //float moveVertical = Input.GetAxis ("Vertical");
    20.  
    21.         transform.position = new Vector3(transform.position.x + moveHorizontal, 0, 0);
    22.  
    23.     }
    24.  
    25. }
     
    Last edited: Mar 8, 2015
  11. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Sorry about that, I knew that you had to store it in a variable, I wasn't thinking. And I typed all of that on my iPhone. But what exactly do you mean by "paddle?" I see you are doing:
    Code (csharp):
    1. float moveHorizontal = Input.GetAxis ("Horizontal");
    Maybe try to minus (-) from that? Like this, maybe?:

    Code (csharp):
    1. float moveHorizontal = Input.GetAxis ("Horizontal") - 5.0f;/* or what ever you want*/
    Edit:
    For the "ResetPosition ()", do this:

    Code (csharp):
    1.  
    2. transform.position = new Vector3 (0.5f, 0.5f, 0.5f);