Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Roll a Ball: ball won't respond to arrow keys. following tutorial

Discussion in 'General Discussion' started by Gabriel.Brokaw, Mar 30, 2016.

  1. Gabriel.Brokaw

    Gabriel.Brokaw

    Joined:
    Mar 30, 2016
    Posts:
    6
    Hey I am following the tutorials for rolling ball and i got to video number 2 and when i hit play there are no problems. Yet the keys don't work!!! Please Help!

    Gabe
     
  2. Gabriel.Brokaw

    Gabriel.Brokaw

    Joined:
    Mar 30, 2016
    Posts:
    6
    Heres the code i used
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    void Awake () {
    rigidbodyComponent = GetComponent<Rigidbody> ();
    if (rigidbodyComponent == null) {
    Debug.LogWarning ("noRigidbodycomponentfound,disabling");
    gameObject.SetActive (false);
    }
    }

    public float speed;

    private Rigidbody rigidbodyComponent;

    void Start()
    {
    rigidbodyComponent = GetComponent<Rigidbody> ();
    }
    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rigidbodyComponent.AddForce (movement * speed);

    }
    }
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What is speed set to in the inspector?
     
  4. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    I tried it, too. I can't figure out how to shoot?
     
  5. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Yeah that's what it looks like to me.
    Aside from that I prefer to use AddRelativeForce. I find using AddForce no matter what sends you in x,y,z not relative to your player, but the world. so if I press D, i go right. Well if I turn 90 degrees and press D, I go fowards. That's how AddForce has always worked for me at least lol.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's how its intended to work in roll a ball. Using add relative force doesn't really work well when your player is a ball rolling in all directions.
     
    N1warhead likes this.
  7. Gabriel.Brokaw

    Gabriel.Brokaw

    Joined:
    Mar 30, 2016
    Posts:
    6
    it is at 10
     
  8. Gabriel.Brokaw

    Gabriel.Brokaw

    Joined:
    Mar 30, 2016
    Posts:
    6
    Yeah the realtive force was a good idea, but it still didn't work.:(
     
  9. AdventurousDrake

    AdventurousDrake

    Joined:
    Mar 18, 2015
    Posts:
    25
    I just finished that tutorial today, it works fine, but I only watched the videos. If you followed the code from somewhere else it might be a leftover from unity 4.
    Here is the final code for my player controller that works.

    EDIT: Check your input manager if you have the keys configured in there. Edit>Project Settings>Input

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     private int count;
    9.  
    10.     public float speed;
    11.     public Text countText;
    12.     public Text winText;
    13.  
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.         count = 0;
    18.         SetCountText();
    19.         winText.text = "";
    20.     }
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         float moveHorizontal = Input.GetAxis("Horizontal");
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.  
    27.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    28.  
    29.         rb.AddForce(movement * speed);
    30.  
    31.      
    32.     }
    33.  
    34.     void OnTriggerEnter(Collider other)
    35.     {
    36.         if (other.gameObject.CompareTag("Pick Up"))
    37.         {
    38.             other.gameObject.SetActive(false);
    39.             count = count + 1;
    40.             SetCountText();
    41.         }
    42.     }
    43.  
    44.     void SetCountText()
    45.     {
    46.         countText.text = "Count: " + count.ToString();
    47.         if (count >= 12)
    48.         {
    49.             winText.text = "You Win!";
    50.         }
    51.     }
    52. }
     
    Last edited: Apr 1, 2016