Blog Post 11| 6/19/2019
I improved the game I made in Unity today. In addition to adding controls that control the directional light in the scene, I also added controls for the player character to move around the environment. Here’s what I have so far:
This is the code I used to move the character around:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 20f;
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Vector3 pos = transform.position;
if (Input.GetKey("w"))
{
pos.z -= speed * Time.deltaTime;
anim.Play("Run");
}
if (Input.GetKey("s"))
{
pos.z += speed * Time.deltaTime;
anim.Play("Run");
}
if (Input.GetKey("d"))
{
pos.x -= speed * Time.deltaTime;
anim.Play("Run");
}
if (Input.GetKey("a"))
{
pos.x += speed * Time.deltaTime;
anim.Play("Run");
}
transform.position = pos;
}
}
Here’s what I used to turn the lights on and off:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsToggle : MonoBehaviour
{
private bool on;
public Light sun;
public float onIntensity = 1.0f;
float offIntensity = 0.0f;
Color color1 = Color.red;
Color color2 = Color.blue;
// Start is called before the first frame update
void Start()
{
sun = GetComponent<Light>();
on = true;
}
public void OnPressed()
{
if (Input.GetKeyDown("space"))
{
if (on)
{
sun.intensity = offIntensity;
on = false;
}
else
{
//float t = Mathf.PingPong(Time.time, onIntensity) / onIntensity;
//sun.color = Color.Lerp(color1, color1, t);
sun.intensity = onIntensity;
//GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
on = true;
}
}
}
// Update is called once per frame
void Update()
{
OnPressed();
}
}
Some ways that I’m thinking about improving this project is by adding a running animation to the character when it moves. I also think that having a larger environment to walk around in and a clearer objective would make for a better game. Today, we also had to choose the Deeper Dive that we are interested in. The one that I most look forward to is GPU programming, as I feel that learning more about topics such as shaders will make me a better computer scientist.
I never knew there was a thing called Mathf.PingPong lol
I didn’t know there was one either until earlier today 😂
Have you ever worked with Unity before? You look like a master compared to me! I got a few shapes to float around, and you’ve got all these moving figures and various camera angles and lighting. Go you! I can’t wait to see the VR Stress Team’s final project.
Thanks! I’ve only worked a little bit with Unity before. In the past, I used tutorials from Brackeys, a youtuber who does a lot with Unity. Just keep working with it, and you’ll learn a lot just by trying out new things
Hey cool project! Thanks for helping us out by providing the code!
Thanks for reading! I think I’m going to eventually put the entire project on Github, so you can check it out once I upload it.
😀