Vignana-Jyothi / vnr-ar-vr

0 stars 0 forks source link

Step3: Player Action implementation & scoreboard building. #6

Closed head-iie-vnr closed 4 months ago

head-iie-vnr commented 4 months ago

Work scope

head-iie-vnr commented 4 months ago

Steps followed

Added below script

I am able to move the ball.

image

head-iie-vnr commented 4 months ago

Created gold coins. Added rotate function to them so that they move vertically.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{

    public Vector3 rotateAmount = new Vector3( 0 , 0 , 1);

    public int speed = 4; 
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
     transform.Rotate(rotateAmount * speed);   
    }
}

image

Now the simulation looks like below image

Change camera angle to make the board look more appealing

image

head-iie-vnr commented 4 months ago

Create a UI Text Element:

In the Unity Editor, go to the top menu and select GameObject > UI > Text. This will create a new Text element in the scene. You should see a new Canvas object and a Text object as a child of the Canvas in the Hierarchy window.

image

Rename the Text Element:

Select the newly created Text object in the Hierarchy. In the Inspector window, rename the Text object to ScoreText.

Position the Text Element:

With the Text object selected, you can adjust its position using the Rect Transform component in the Inspector. You can also adjust the size, font, color, and other properties to make the score display as you like.

Fit it so that it looks like below image

head-iie-vnr commented 4 months ago

Create an Empty GameObject for the ScoreManager:

In the Hierarchy window, right-click and select Create Empty. This will create a new empty GameObject. Rename this GameObject to ScoreManager.

Add ScoreManager Script

Create the ScorerManager.cs script in the Assets > scripts.

using UnityEngine;
using UnityEngine.UI;

using TMPro; 

public class ScoreManager : MonoBehaviour
{
    public static ScoreManager instance;
    public TextMeshProUGUI scoreText;
    private int score = 0;

    void Awake()
    {
        Debug.Log("Called Score Manager");
        if (instance == null)
        {
            instance = this;
            Debug.Log("new instane is created");
        }
        else
        {
            Destroy(gameObject);
        }
    }

    void Start()
    {
        UpdateScoreText();
    }

    public void AddScore(int amount)
    {
        score += amount;
        UpdateScoreText();
    }

    void UpdateScoreText()
    {
        scoreText.text = "Score: " + score;
    }
}

Attach the ScoreManager Script:

With the ScoreManager GameObject selected, go to the Inspector window. Click on Add Component and search for the ScoreManager script to attach it.

image

Assign the ScoreText to the ScoreManager:

In the Hierarchy window, select the ScoreManager GameObject. In the Inspector window, you should see the ScoreManager script component. There should be a field named ScoreText. Click the small circle next to this field to open the object picker. Select the ScoreText object from the list.

Got error: Assets/scripts/ScoreManager.cs(7,12): error CS0246: The type or namespace name 'TextMeshProUGUI' could not be found (are you missing a using directive or an assembly reference?)

Imported image

Quick check on Windows > Package Manager reveals that it was not there. image

I tried installing using URL image

Now you should be able to attach the variable to the image

head-iie-vnr commented 4 months ago

Enable triggers

Ball Inspector

Ensure that Ball has 'Sphere Collider' enabled. But Is Trigger disabled.

Coin Inspector

Select all coins and check in the inspector that it has 'Cylendar Collider'.
Enable the Is Tigger attribute.

Definitions

Rigidbody Component:

A Rigidbody is usually added to the ball to enable physics-based movement and interactions.

Collider

A Collider is used to define the physical boundaries of an object for collision detection. Colliders can be of various shapes, such as boxes, spheres, capsules, meshes, etc.

Is Trigger Property

When the Is Trigger property of a collider is enabled, it changes how the collider interacts with other colliders:

Is Trigger Disabled (unchecked):

The collider acts as a solid object. Physical interactions like collisions will occur. For example, a ball will bounce off a wall. Collision events like OnCollisionEnter, OnCollisionStay, and OnCollisionExit are used. Is Trigger Enabled (checked):

The collider becomes a trigger area.

Objects can pass through the trigger collider without physical collision. Trigger events like OnTriggerEnter, OnTriggerStay, and OnTriggerExit are used to detect when other colliders enter, stay within, or exit the trigger area.

In Unity, the Rigidbody component is used to make an object respond to physics. The Rigidbody component includes several properties that control how physics is applied to the object. Here’s an explanation of some key properties:

These properties help control the physics behavior of objects in your Unity scene, allowing for realistic movements and interactions.

head-iie-vnr commented 4 months ago

Now the board looks like below

image

Known issues to be fixed later

Next Features