UnityNote1

Unity Learning Note Part 1

I’d been learning on Unity for about 3 weeks, it’s time to write something down. If I take a review, that first time I launch this can be rooted to 2012, cause I have a purchase record in unity store. I don’t know what if I can insist it this time. Just like I finally gave a step forward to the gym.

Coding Skills

Catch keyboard input

1
2
3
4
5
6
7
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
//The code below shows how a character change its position and rotation flow the movement from the keyboard input
/* the new movement which is generated by the keyboard,
* alternatively I can catch emu joystick,
* physical keyboard from mobile device too.*/
movement = new Vector3 (moveHorizontal,0.0f,moveVertical);

This piece of code is running in MonoBehaviour subclass Update function.

To move GameObject smoothly

I apparently think there should be something like Tweener that I’ve be using in many other different languages, but from Nick, he prefers to use the enter-frame based way to do it, that seems like this.

1
2
3
4
5
6
7
8
9
10
11
12
// If the characters movement does not equal zero
if (movement != Vector3.zero) {
// create target rotation
Quaternion targetRotation =
Quaternion.LookRotation(movement,Vector3.up);
// and create another rotation that move lerp
Quaternion newRotation =
Quaternion.Lerp(characterRigidbody.rotation,targetRotation,
turningSpeed * Time.deltaTime);
// apply the rotation and movement to character
characterRigidbody.MoveRotation(newRotation);
}
  • The characterRigidBody equals to the hard part of a real object which can react the physical powers.
  • The turningSpeed is the speed for character rotation, It can be adjusted what ever you like. The value here is 20f.
  • Function Lerp(), is the way which Unity use to make step change from one value to another. It is exiting in a lot API Classes like Color.Lerp(), can make a color change from one to the other gradually.
  • Time.deltaTime , The time in seconds it took to complete the last frame.
  • For performance reason, this piece of code is running in MonoBehaviour subclass FixedUpdate function.

Acting touch/click action on the GameObject

The way I know now:

1
2
3
4
5
6
void OnMouseUp(){
//Check if the action is from the GameObject but not the UI Element
if (EventSystem.current.IsPointerOverGameObject ()) {
Debug.Log("Touch me!!");
}
}

Act trigger

Trigger is the way for GameObject hit test. It is called when the Collider other enters the trigger.

1
2
3
4
5
6
void OnTriggerEnter(Collider other){
// the way to detect the role of the other.
if (other.CompareTag ("Enemy")) {

}
}

UI Tool Skills

SerializeField

A SerializeField property is the way to connect different parties in the project, like, if you wanna a bird look at you, first declare a transform property in Bird Script like this:

1
2
[SerializeField]
private Transform target;

Second, point the target to the Player instance in the scene by dragging or select tool.

Add 2D UI element on the Screen

Add a Canvas to the Scene just like you did for all the 3D Object, right click hierarchy window to create a Canvas, then create any sub-element in it.
The way to make 2D Content full-size of screen.

Add RigidBody to GameObject

Notice add RigidBody is not enough to make the object react the collider in the scene. After import you 3d model in to the scene, select it and add component in the inspector window.

Then add a appropriate collider by add component too.

What can I do if the GI doesn’t work.

I answered this on teamtreehouse

Trouble

WebGL and Unity Web Player

Unfortunately(but fortunately for unity’s road map), unity decided to stop the support to Unity Web Player after version 5.3. Instead of that, it will be implemented by WebGL, but from my current state, WebGL’s performance sucks.

Our company is trying to bid a project which need to be show on Sogou and 360 browser. To do that, I need to downgrade my env to 5.3 to publish a web player version (the shadows of flash player come again).

What I really want to do is a game runn in VR/AR. I think I can make it.

Requirement that need to be accomplished.

I made a list that is all I need to learn unity & 3d modeling.

  • A Powerful PC 7000¥
  • Card Board DoDoCase 17.5$ 120 ¥
  • 3D Art Course CGCookies 239$
  • Logitech Wireless Marathon Mouse M705 (29$ Amazon)(289¥ jd.com)
  • SteelSeries Nimbus 36$ used

Others

What moves me a lot to start this track is, I and old enough to do something.
I started training 5 time per week, I aware how weak my body is.
I took my wife to the gym yesterday to share our memories. Before that we went to watch the movie La la Land. I cried when you know two lovers can’t be together, and the last few minutes was all rendering what if they belong to each other, that really got me moved.