1. Create variables
I created 5 variables of different types:
public float speed = 1.0f; public string name = "Cube"; public int age = 10; public bool isDead = false; public char ID = 'a';
Then in void Start() , I printed them so that they could be shown in the console part.
print("the name is " + name); print("the speed is " + speed); print("the age is " + age); print("Is it dead? " + isDead); print("the ID is " + ID);
This is the result shown in the console part when running it in Unity project:
2. Small story
Firstly I created stored these information in 10 variables of proper type:
public string name1 = "Samantha"; public string name2 = "Jack"; public int age = 17; public string vehicle = "tricycle"; public string location = "lakeside"; public int ammo = 30; public string animal = "a Mocking Bird"; public string direction = "the Moon"; public string color = "blue"; public bool isTall = false;
Then I printed the story and here is a part of the code showing what have I done:
print($"{name1} and {name2}, both {age}, spent their summer days exploring the serene {location} near their hometown. " + $"One sunny afternoon, armed with {ammo} rounds of excitement and riding their trusty {vehicle}, they embarked on a whimsical journey. " + $"Their destination? The perfect spot to admire {direction}.\n");
This is the video showing the text shown in the console part in Unity:
3. Car speed control 1
This is the video showing the moving car challenge:
4. Car speed control 2
To control the speed of the car in a certain range, I add more condition in if statement. Here is the video showing the moving car challenge with limited range of speed:5
5. Car speed control 3
To show the states when the speeds are different in the console part, I add some if statements when printing the speed.
speed in (5,∞) ——— overspeed
speed in [0,5) ——— moving smoothly
speed in {0} ——— stopped
speed in (-∞,0) ——— reversed
This is the video showing the final result:
This is the finalized version of code:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class move : MonoBehaviour { public float speed = 1.0f; public Rigidbody rb; private Transform _transform; // Start is called before the first frame update void Start() { _transform = transform; } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.W)) { speed += 1f; if (speed > 5) { print("The current speed is " + speed + ", overspeed."); } else if (speed <= 5 && speed > 0) { print("The current speed is " + speed + ", moving smoothly."); } else if (speed == 0) { print("The current speed is " + speed + ", stopped."); } else if (speed < 0) { print("The current speed is " + speed + ", moving in a reversed direction."); } } else { if (Input.GetKeyDown(KeyCode.S)) { speed -= 1f; if (speed > 5) { print("The current speed is " + speed + ", overspeed."); } else if (speed <= 5 && speed > 0) { print("The current speed is " + speed + ", moving smoothly."); } else if (speed == 0) { print("The current speed is " + speed + ", stopped."); } else if (speed < 0) { print("The current speed is " + speed + ", moving in a reversed direction."); } } } rb.Move(transform.position + transform.forward * speed * Time.deltaTime,_transform.rotation); } }