Saturday, July 4, 2020

TOGGLE CAMERA ANGLES IN UNITY 3D

I'm currently going through a tutorial on how to build a racing game, and I'm having to fix/improve many things in the tutorial, including camera angles, because it's an old tutorial. The tutorial I'm referring to is by Jimmy Vegas, and I left a link to his video in this post.

Jimmy's tutorials are made for absolute beginners, so it's good to take his code, and see how you can improve on it.

CREATE DIFFERENT CAMERA OBJECTS

Before you can switch between different camera angles, you need to create separate objects that each have a camera component attached to them. Notice that FarCam and FPCam are grayed out. That's because I'm using NormalCam as default, and when we switch cameras, the previous one will be disabled and the next one gets enabled.


CREATE C# SCRIPT

You'll need to create a C# script to effectively enable and disable the camera objects. Below is a drastically modified version from the one in the tutorial.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraChange : MonoBehaviour
{
    public GameObject[] camModes;
    private int currentCamMode;
    private bool buttonPressed = false;

    // Update is called once per frame
    private void Start()
    {
        currentCamMode = 0;
    }

    void Update()
    {
        if (Input.GetButton("viewmode") && !buttonPressed)
        {
            buttonPressed = true;
            StartCoroutine(CamChange());
        }
    }

    IEnumerator CamChange()
    {
        yield return new WaitForSeconds(0.5f);
        camModes[currentCamMode].SetActive(false);
        currentCamMode++;
        if (currentCamMode == camModes.Length)
        {
            currentCamMode = 0;
        }
        camModes[currentCamMode].SetActive(true);
        buttonPressed = false;
    }
}

camModes: Instead of creating three separate variables for each camera, I just created an array to hold them all.

buttonPressed: This variable is important and didn't exist in the tutorial. Without this variable, the camera would change would trigger multiple times per frame, making it very glitchy. Basically, in the update method, if that variable is true, it will not try to change the camera again.

Start(): I just use this method to set the current camera to 0 (NormalCam). This method only gets called once, so this is a good place to set default variables.

CamChange(): First we wait half a second (adjust this as you see fit). Then we disable the camera we're currently using (currentCamMode). We then increment currentCamMode by 1, so if we were on NormalCam(0), we'd now be on FarCam(1). The next line prevents an "Index out of bounds" error from occurring and resets the currentCamMode variable to 0. With our updated currentCamMode variable, we use it to enable the next camera object. And finally, we set the buttonPressed variable to false, which allows us to switch camera angles again.

ADD SCRIPT TO EMPTY OBJECT

Create an empty object and call it whatever you want, something like CameraManager. Take that above script, and drag it onto that object. You should see a camModes variable with a size parameter. Set the size to 3 or however many camera objects you have.
Finally, just drag each camera object into each element. That's it! Assuming you have your "viewmodes" key set, all should work. If you're having trouble, let me know in the comments, and I'll help you out.

Sunday, August 30, 2015

Drupal Commerce: Update Totals When Quantity Is Changed

This is just an example on how to implement ajax on the drupal commerce line items form. For this to work on your project, you'll need to make some significant changes, but this should at least give you a good start.

Saturday, May 9, 2015

How To Make A Drupal Module: Part 1

Creating a Drupal Module is easy! At the minimum, you only need two files; a .info file, which describes our module and a .module file, which is where most of our code goes.

First things first, you need to create a folder to place our two files in. Call it my_module and place it in sites/all/modules (I place mine in sites/all/modules/custom).

Pass JSON To Drupal Page

I worked on a complex javascript application that comprised of many checkboxes, textfields, dropdowns and multiple steps. The data needed to be stored in a database and since I didn't use Drupal's Ajax, I needed to pass the data asynchronously elsewhere for that to be handled.

Saturday, February 28, 2015

View Exposed Filter with a dropdown/select and textfield

In this example, we'll edit the commerce orders view. We need to edit the exposed filters in the view's configuration so we can then modify it programmatically and add more form elements. Without a preexisting exposed filter, there won't be a form to modify.

Sunday, June 8, 2014

Freelancer Drupal Exam (some questions)

If you're like me before taking an exam, you probably like to get an idea of what questions will be in it. As far as the Drupal Exam, I found nothing. I hope this post will help some people. I'm only posting the first 12 questions (keep in mind the exams are random). I do need to take the exam again if I want a better score, so more questions may be posted later.

Thursday, March 20, 2014

Drupal Commerce Bulk Order Printing

You will need to download and enable Views Bulk Operations and Commerce Invoice Receipt for this solution. This also assumes you know how to create a basic module.

Thursday, November 7, 2013

Drupal Views Related Products

This method requires Views PHP in order to select the appropriate products.

There are many articles on this, but none worked perfectly for me, so I'm writing the steps I took.