« 包含标签 UNITY3D 的文章

Spin like “Google Earth” in Unity

It’s complicated than I thought…

#pragma strict
 
var numberAverages : int = 3;
private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;
 
// Make sure there are always SphereCollider and Rigidbody
@script RequireComponent (SphereCollider)
@script RequireComponent (Rigidbody)
 
function Awake () {
    numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
}
 
function OnMouseDown () {
    var hit : RaycastHit;
    var dir : Vector3;
 
    // Stop spinning
    rigidbody.angularVelocity = Vector3.zero;
 
    // Record initial variables 
    if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        originalRotation = transform.rotation;
        dir = hit.point - transform.position;
        offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
        Spin (dir);
    }
}
 
function Spin (dir : Vector3) {
    var hit : RaycastHit;
    var privDirList : Array = new Array ();
    var currentDir : Vector3;
 
    // Initialize previous direction list
    for (var i : int = 0; i < numberAverages; i++) {
        privDirList.Add (currentDir);
    }
 
    currentDir = dir;
 
    // Make the object rotate with the cursor while we are grabbing it
    while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // Remove first element of the array
        privDirList.RemoveAt (0);
 
        // Add current direction to the end
        privDirList.Add (currentDir);
        currentDir = hit.point - transform.position;
        transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
        yield;
    }
 
    // User let go of the mouse so make the object spin on its own
    var avgPrevDir : Vector3 = Vector3.zero;
    var newPDirList : Vector3[] = privDirList.ToBuiltin(Vector3);
    for (dir in newPDirList) {
        avgPrevDir += dir;
    }
 
    // Keep spinning with the direction when you end touching
    avgPrevDir /= numberAverages;
    MoveAcceleration (currentDir, avgPrevDir); 
}
 
function MoveAcceleration (r2 : Vector3, r1 : Vector3) {
    var linearVelocity : Vector3;
    var angVelocity : Vector3;
 
    // Calculate the angular velocity:  Omega = r x v / r^2
    linearVelocity = (r2 - r1) / Time.deltaTime;
    rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
}

how to create instance in Unity

#1 Create Class Car.

using UnityEngine;
using System.Collections;
 
public class Car : ScriptableObject {
	public string color;
 
	void Awake(){
		color = "red";
	}
}

#2 Create instance.

using UnityEngine;
using System.Collections;
 
public class main : MonoBehaviour {
	void Start(){
		Car polo = (Car) ScriptableObject.CreateInstance(typeof(Car));
	}
}

example of virtual function in unity3d with C#

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using UnityEngine;
using System.Collections;
using System;
 
public class car : MonoBehaviour {
    private A a;
    private A b;
    private A c;
    private A d;
    private D e;
    private C f;
 
    void Start () {
        a = new A(); 
        b = new B(); 
        c = new C(); 
        d = new D();
        e = new D();
        f = new C();
 
        a.Func(); // print A
        b.Func(); // print B:A
        c.Func(); // print B:A  
        d.Func(); // print A
        e.Func(); // print D:A
        f.Func(); // print B:A
    }
 
 
    public class A {
    	public virtual void Func() 
        {
            Debug.Log("A");
        }
    }
 
    public class B : A  
    { 
        public override void Func()
        { 
            Debug.Log("B : A");
        }
    }
 
    public class C : B 
    {
 
    }
 
    public class D : A 
    {
        public new void Func()
        {
            Debug.Log("D : A");
        }
    }
 
}

keyframe control of 3D model Animation in Unity3D

Here’s a simple sample of controlling model animation in Unity3d.

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
using UnityEngine;
using System.Collections;
 
public class animationCtrl : MonoBehaviour {
	private float hSliderValue;
	private AnimationState currentAni;
 
	void Start () {
	    gameObject.animation.Play("shot");
	    gameObject.animation.Stop();
	    currentAni = animation["shot"];
	}
 
	void LateUpdate () {
	    currentAni.time = hSliderValue;
	    currentAni.enabled = true;
 
	    animation.Sample();
	    currentAni.enabled = false;
	}
 
	void OnGUI() {
	    hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0f, currentAni.length, GUILayout.Width(100));
	}
}

Unity + Flash coming soon…

Unity’s Flash Player addon leverages the new Stage3D APIs in Adobe Flash Player. Reach the widest possible audience with GPU-accelerated 3D content on the web with games and interactive content created in Unity.

>> Link

Rochard by Recoil Games

Share a Unity3d game. 🙂

Recoil Games discusses the use of Unity in the production of Rochard, a side-scrolling action-platformer that combines of smart puzzles and engaging mechanics to be one of the most critically-acclaimed PSN titles to date.

Recoil Games 制作发行的一款3D画面、2D横版过关的动作物理冒险游戏《理查德》,大家不要被游戏中欧美风格的大叔、普普通通的画面所影响到试玩本作的心情,游戏因其独特的重力系统设定而变得非常有趣。 游戏故事简介:主角理查德(Rochard)是一位外太空采矿自由职业者,某天他在某星球发现了外星生命的证据,于是进入探险。在冒险过程中,理查德了解了外星文明、并且认识了外星女人以及女人与外星恶霸之间的悲伤故事,拯救美女,解放外星就成了主角的目的。 游戏画面:有点卡通渲染味道、3D背景2D游戏方式,欧美游戏建模略显粗糙,控制理查德来回左右走动时,视角的变换有些快,多个来回之后有点容易头晕…爆炸效果和烟雾效果都很棒,场景蛮带高科技感。 游戏特色:主角手中的重力枪是核心道具。场景中的各种道具都可以吸附,可以垂直放下物体,可以控制这个抛物线。 游戏精髓操作:理查德可进入失重形态,这时候跳跃起来的高度将会增加,很多高地就能跳上去。不过还有更高的台阶,怎么办呢?没关系,因为反作用力的关系,玩家们会被箱子推起来,这样就能到达更高更远的地方了。 除了动作解密要素之外,游戏还有更版清版射击的要素存在,除了普通的武器攻击之外,用还能使出手榴弹、C4强力炸弹等攻击手段。