- Home /
Move a ball using keybored
Am trying to make a ball move by using my arrow or WASD keys. But I can't code 'at' all. How would one come across to do this? I have a Sphere with a "sphere collider" using Rubber material and the Sphere also has a rigid body. Thanks I will try and learn code but I can't :(
Yeah, my keys are really bored. $$anonymous$$aybe moving a ball would fix it.... I'm sorry, I had to xD
Answer by BiG · Oct 05, 2011 at 06:33 AM
You can use this script attached to your object to move it with arrow keys:
var speed = 5.0;
function Update () { var x = Input.GetAxis("Horizontal") Time.deltaTime speed; var y = Input.GetAxis("Vertical") Time.deltaTime speed; transform.Translate(x, y, 0); }
If you are trying to learn coding, I would explain it to you: First at all, speed is declared outside the function Update, so, it's an "exposed variable": you can access it from another script and even modify it from the Inspector View (attach it to the ball, click on the ball, and look in the right section of the screen: See "speed" value among all parameters of the ball?
Then, go to the function: it's an Update function, so it means that all the instructions inside it are performed every frame (other types of function exist: for example, function Start is performed once when the object is created).
Now, go to third instruction: "transform.Translate" translates the object in the point defined by x,y and z coordinates (z is 0 in my example).
How we defined x and y? By the 2 instructions above: considering x, we obtain it from the pressure of the related arrow key (Horizontal means left and right arrows, naturally), and from the speed time, that depends by the exposed variable "speed".
Try making experiments changing speed and the coordinate system (maybe, translate it on x and z, instead than x and y).
Note that, if you wanna use WASD, you need the Input.GetButtonDown function instead than Input.GetAxis. Look here: http://unity3d.com/support/documentation/ScriptReference/Input.GetButtonDown.html
The link that I've just reported it's a sublink of the scripting reference on the official site (a kind of scripting manual, in other words): you can learn a lot from it, so keep it in mind, when you need for an instruction!
I have a question about that. Does it work on Unity 4 or do I have to upgrade it to Unity 5? I did everything according to the 2nd tutorial video, but it's not working.
Answer by EvonneMeadows · Jul 01, 2015 at 05:32 AM
using UnityEngine; using UnityEditor; public class FindMissingScriptsRecursively : EditorWindow { static int go_count = 0, components_count = 0, missing_count = 0;
[MenuItem("Window/FindMissingScriptsRecursively")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(FindMissingScriptsRecursively));
}
public void OnGUI()
{
if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
{
FindInSelected();
}
}
private static void FindInSelected()
{
GameObject[] go = Selection.gameObjects;
go_count = 0;
components_count = 0;
missing_count = 0;
foreach (GameObject g in go)
{
FindInGO(g);
}
Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
}
private static void FindInGO(GameObject g)
{
go_count++;
Component[] components = g.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
components_count++;
if (components[i] == null)
{
missing_count++;
string s = g.name;
Transform t = g.transform;
while (t.parent != null)
{
s = t.parent.name +"/"+s;
t = t.parent;
}
Debug.Log (s + " has an empty script attached in position: " + i, g);
}
}
// Now recurse through each child GO (if there are any):
foreach (Transform childT in g.transform)
{
//Debug.Log("Searching " + childT.name + " " );
FindInGO(childT.gameObject);
}
}
}
Answer by CBRZY · Jun 07, 2016 at 08:16 AM
Ok, obviously it would go a bit faster and be a bit easier if you have some coding experience, but don't worry, in my opinion Unity is probably the best engine to use in your case, because you will be able to make proper games without having to be a coding guru.
First of all, take a look at this tutorial for Unity, the create a game where you are moving a ball around with the keyboard https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial With this tutorial they also link the Unity manual when using Unity specific functions and features.
I would advise you to go through this live training session first though https://unity3d.com/learn/tutorials/topics/scripting/coding-unity-absolute-beginner This teaches you the basics of programming in the Unity IDE.
If you finish this training session and the tutorial on the rolling ball and you still enjoy it, then making games might be for you. Remember that coding isn't that easy. It will take some struggles to understand why something is or isn't working sometimes. This is just part of it, but don't give up, it gets easier... sometimes.
Good luck