To develop a 3D application for Roll A Ball in unity.
Create a new project.
Click Heirarchy -> 3D object -> Select the plane -> 3DObject -> Sphere.
Define the physics properties of the surface (Rigidbody).
Assets -> Create -> # Script
Create a folder name Coding and create a C# file to add the coding in it.
To add our C# Script file to our selected object, click on the C# Script file and drag it to our selected objects in the Hierarchy window nad run the application.
Stop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RollingBall : MonoBehaviour
{
// Start is called before the first frame update
public float xforce = 5.0f, yforce = 200.0f, zforce = 5.0f;
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = 0.0f, y = 0.0f, z = 0.0f;
if(Input.GetKey(KeyCode.A))
{
x = x - xforce;
}
if(Input.GetKey(KeyCode.D))
{
x = x + xforce;
}
if(Input.GetKey(KeyCode.W))
{
z = z + zforce;
}
if(Input.GetKey(KeyCode.S))
{
z = z - zforce;
}
if(Input.GetKeyDown(KeyCode.Space))
{
y = yforce;
}
GetComponent<Rigidbody>().AddForce(x, y, z);
}
}
Thus, a 3D application for RollABall objects in unity is developed successfully.