#CGDD Class01 Assignment -3: C# Challenges

Challenge 1:

Challenge2:

Challenge3:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class challenge3 : MonoBehaviour
{
public float speed = 1.0f;

public Rigidbody rb;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
speed ++;
print(speed);
}
else if (Input.GetKeyDown(KeyCode.A))
{
speed --;
print(speed);
}
rb.Move(transform.position + transform.forward * speed * Time.deltaTime, transform.rotation);
}
}


Challenge4:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class challenge4 : MonoBehaviour
{
public float speed = 1.0f;
public Rigidbody rb;
private float maxspeed = 5.0f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.D) && speed 0)
{
speed --;
print("The speed is " + speed);
}
rb.Move(transform.position + transform.forward * speed * Time.deltaTime, transform.rotation);
}
}


Challenge5:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class challenge5 : MonoBehaviour
{
private float maxspeed = 5.0f;
public float speed = 1.0f;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
speed ++;
}
else if (Input.GetKeyDown(KeyCode.A))
{
speed --;
}
rb.Move(transform.position + transform.forward * speed * Time.deltaTime, transform.rotation);
print("The speed is " + speed);
if (speed > maxspeed)
{
print("state: Overspeed");
}
else if (speed > 0)
{
print("state: Moving");
}
else if (speed == 0)
{
print("state: Stop");
}
else
{
print("state: Reverse");
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *