- Home /
Controlling a sphere object
I have been trying to make a script that could control a sphere efficiently as a player in the 3D Space without any ground. So far I have tried using rigidbody.addrelativeforce and use the if(input.getkey) to detect if the arrows are pressed to move the sphere around with a code transform.position = transform.position + new Vector3(0,5,0); but when I use that it makes the sphere more violently and it appears like it went up then went back down (but it stays up as I can see the position in inspector)(i have the smoothfollow camera)
and I have tried using a cube as a head and make it rotate when up key is pressed but that doesn't help either
I would really appreciate a help on this. Thank you.
Answer by GC1983 · Feb 22, 2013 at 08:49 AM
public float speed;
public CharacterController charController;
void Update()
{
var moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
charController.Move(moveDirection * Time.deltaTime * speed);
}
Make sure you have a Character Controller attached. Assign the Char Control to the script. Go to Edit -> Project Settings -> Input. Go down to Vertical and Horizontal. Make sure theyre set to Key and Mouse, and then you can the hotkeys and alternate hotkeys of what each direction will do. You can set it to be directly 2D direction, or 3D.
Youre just having total misunderstanding of what the code does. There are tons of links that relate to this situation. This is common Unity referencing practice.
Hope this helps.
works like magic but what I want to do here is let the sphere move in the Z axis itself with some set velocity and move it freely in the other two axis, that is the sphere will go forward automatically while the user control its up-down and left-right movement
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html
$$anonymous$$aybe set a velocity in one direction and then create the GetAxis to move the other directions that doesnt conflict with its path.
rigidbody.velocity = new Vector3(xVect, yVect, zVect);
How would you define the Vertical axis such that ins$$anonymous$$d of moving in Z axis it moves up in the Y axis, if that could be done I can use rigidbody.addrelativeforce to move the sphere in Z axis in the way I would like.
Thats a setting in the project settings. You can have the settings to move in 2d or 3d space.