This week we were assigned our first assignment. We had to create a simple 3D game with 3 levels, pickups and triggers. I went for a 3rd person shooter game. During this first week, I got the 3rd person camera done, as well as the player controller, 2 abilities and basic enemy.
For the 3rd person camera script, I first got the mouse x and mouse y inputs and put them into a vector3. Then that rotation was applied to the camera parent's euler angles. This creates a rotating camera, yet looking too far up or down glitches it out.
To the right of the image are the 2 abilities. One is a jetpack and the other is a shrink ability. In the levels the player will need to use these to get past obstacles. When on the ground, these abilities have a particle effect, purple light and bobbing effect. This effect makes the ability go up and down as well as rotate. This makes it easier for the player to see.
//Do we bob up? if(bobUp){ transform.position = Vector3.MoveTowards(transform.position, bobToPos, bobSpeed * Time.deltaTime); //Are we close to the peak? if(Vector3.Distance(transform.position, bobToPos) <= 0.1f){ bobUp = false; } }else{ transform.position = Vector3.MoveTowards(transform.position, defaultPos, bobSpeed * Time.deltaTime); //Are we close to the bottom? if(Vector3.Distance(transform.position, defaultPos) <= 0.1f){ bobUp = true; } } //Rotating the object. transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
This is the code for the Bob.cs script and it's quite simple. It moves the object up and down around one unit in distance and rotates it at a certain speed. Instead of a static object sitting there, this bobbing object attracts the eyes of the player and makes the game look even better.
Also in the image, but not seen is shooting the enemy. The player can shoot spherical projectiles, but a problem that I occured is where do they shoot towards? Obviously ahead of the player, but since it's a 3rd person camera where does the player aim? Aiming with the crosshair is a problem since the projectile is shooting at an angle towards the crosshair and only crosses it at a single point.
So what I decided to do was have the crosshair shoot a raycast and if it hit an enemy, just have the projectile fire towards the enemy. If it wasn't an enemy, then the projectile would move in that general direction. Most 3rd person games don't need to worry about this since they shoot with raycasts only, yet with this project I was using physical objects.
//Calculating the direction the projectile will travel. if(Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out hit, 100)){ hitPos = hit.point; } Vector3 dir = Vector3.zero; if(hitPos == Vector3.zero){ Vector3 camDir = (transform.position - Camera.main.transform.position).normalized; dir = new Vector3(camDir.x - 0.3f, camDir.y + 0.45f, camDir.z); }else{ dir = (hitPos - transform.position).normalized; }
This is the code and it does what I mentioned before. I wouldn't say this is the best method since it's really hacky and doesn't even look that good. Although for what I need it for, it will do.