Unity 4 – Experimenting with Cars

September 05 / 2014

Followed this tutorial

http://forum.unity3d.com/threads/basic-to-advanced-car-or-vehicle-tutorial.86703/

Some things to keep in mind that was not mentioned in the tutorial

  1. The forward direction of your car is the Z axis – make sure all object Z axis are aligned with the word Z axis during setup
  2. Make sure the wheel collider gizmo line is pointing from the center down – the direction of the ground.
  3. When adding the wheels with wheel colliders to the CarController1 wheels array start with; frontRight, frontLeft, rearRight, rearLeft
  4. Fix camera smooth follow issues by parenting an empty to the car and using the empty as the Target for the camera to follow

Another thing to consider is scale and unit setup; 1 Unit in unity is supposed to represent 1 Meter. That makes our car 5 meters long and 3 meters wide; not to realistic.

Also there is the issue of the car rolling over around corners. So far the most practical solution would be to implement stabilizaton as described in the following tutorial.

http://forum.unity3d.com/threads/how-to-make-a-physically-real-stable-car-with-wheelcolliders.50643/

 

Posted in Uncategorized, Unity3D | Leave a comment

Unity4 Scripting – Choose / Select from Array

A simple script that cycles through an array

using UnityEngine;
using System.Collections;

public class Selector3 : MonoBehaviour {

public GameObject[] selectionSet;
GameObject selection;
//GameObject nextSelection;
//GameObject previousSelection;
int index = 0;

//Vector3 previousPosition = new Vector3(-1,0,0);
Vector3 selectionPosition = new Vector3(0,0,0);
//Vector3 nextPosition = new Vector3(1,0,0);

// Use this for initialization
void Start () {

selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject;

}

// Update is called once per frame
void Update () {

if(Input.GetKeyDown(KeyCode.RightArrow)){

Destroy(selection);

if(index < (selectionSet.Length – 1)){ index++; } else { index = 0; } selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject; } if(Input.GetKeyDown(KeyCode.LeftArrow)){ Destroy(selection); if(index > 0){
index–;
} else {
index = (selectionSet.Length – 1);
}

selection = Instantiate (selectionSet [index], selectionPosition, Quaternion.identity) as GameObject;
}

}
}

Posted in Uncategorized | Leave a comment

Objects First With Java 5E – Chapter 01 Notes

Objects are categorized by class.

Objects may be categorized as – and a class describes, in an abstract way – all objects of a particular kind.

ie ; Car is the class and ford focus an object of the Car class.

The word “instance” is synonymous with the word “object”, and will be used interchangeably.

ie : ford focus is an instance of the Car class

Convention : name of a class should start with a capital letter and name of an object or instance of that class should start with a lowercase letter.

ie : fordFocus is an object instance of the Car class

Methods represent operations that can be used to manipulate objects. We communicate with objects by calling or invoking methods

The word “function” is synonymous with the word “method” and will be used interchangeably.

Sometimes methods require parameters to provide additional information for a task.

The top line of code for a method is the header and is known as the signature for that method. in this case void setCarType(String newCarType) is the method signature.

void setCarType(String newCarType)
    {
        carType = newCarType;
    }

Parameters have data types. Data types define what type of data will be passed into method as parameters.

ie : int, String, double, boolean

There are two kinds of data type : primitive data type and object data type.

A variable is created and the primitive data type is stored directly in that variable

An objects exists and a reference to that object is stored in the variable to make that variable an Object data type

You may create multiple instances of objects of a single class.

Obects have state. State is represented by values in fields that define the object. All the data values together in an objects fields are referred to as an object’s state.

The word “field” is synonymous with the word “instance variable” and is used interchangeably.

Objects of the same class have the same number, name and type of fields.

Objects of the same class have the same number, name, and type of methods.

The following is an example of creating a new object of the Car class and calling on a method of that object.

Car car1 = new Car();
car1.setCarType();

On the first line we create the car1 object by first declaring it and assigning it to new Car object.  On the second line we call a method on that object by first typing the object name followed by a dot, followed by the method name, followed by a parameter list.

Parenthesis indicate a parameter list. Anything within the parenthesis is considered part of the parameter list.

All java statements end with a semi colon ;

Method Calling. Objects can communicate by calling eachothers methods.

The source code of a class determines the structure and behavior (the fields and the methods)of each of the objects of that class.

Methods may return infomation about an object via a return value.

Return type is that data type that is returned as a result of calling a method

The return type is specified in the signature of the method. If a void return type is specified then the method does not have a return statement and does not return any data.

public String getCarType()
    {
        return carType;
    }

In the previous code we see String specified as the return type in the method signature. We also see the return statement in the body of the method.

Objects can be used as parameters as long as the object class is specified as the object data type in the method header.

public void addCar(Car newCar)
    {
        carList.add(newCar);
    }

Exercise Questions

1.14 How do you think the House class draws the picture?

1.14 The House class calls the draw() method that in turn creates instances of the objects needed to form the picture.

1.33 Write the signature for a method named send that has one parameter of type String, and it does not return a value.

public void send(String msg)

1.34 Write a signature for a method named average that has two parameters both of type int, and returns an int value.

public int average(int firstInt, int secondInt)
Posted in java, object oriented programming, programming | Leave a comment