To Roll a Ball using C# program in unity.
To Create a unity project in unity hub.
Aftern creating Click File -> Scene -> Select the scene -> Save as-> New folder(Scenes)-> File name (Expno2).
Click Hierarchy -> 3DObject -> Sphere Hierarchy -> 3DObject -> plane Hierarchy.
Create a folder in project and name as Materials Material folder -> Create -> Material (Name: Sphere) Inspector ->Surface Inputs ->BaseMAp (Choose the color) Drag the Cylinder to the plane and release the mouse
Create a folder in project and name as Materials Material folder -> Create -> Material (Name: Plane) Inspector ->Surface Inputs ->BaseMAp (Choose the color) Drag the Capsule to the plane and release the mouse
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 Sphere objects in the Hierarchy window and run the application.
After run the application, you press the WASD and space key the ball will move and jump.
Save the unity file and take screenshot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class kayal : MonoBehaviour
{
public float xForce = 5.0f;
public float zForce = 5.0f;
public float yForce = 200.0f;
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = 0.0f;
if(Input.GetKey(KeyCode.A))
{
x = x - xForce;
}
if (Input.GetKey(KeyCode.D))
{
x = x + xForce;
}
float z = 0.0f;
if (Input.GetKey(KeyCode.S))
{
z = z - zForce;
}
if (Input.GetKey(KeyCode.W))
{
z = z + zForce;
}
float y = 0.0f;
if(Input.GetKeyDown(KeyCode.Space))
{
y = yForce;
}
GetComponent<Rigidbody>().AddForce(x, y, z);
}
}
Thus, the 3D application for Roll the Ball objects in unity is developed successfully.