0
OVERALL GOAL: Have the camera change target to the selected car

我是统一游戏引擎的新手,遇到了一些问题。

所以,我有一个成功的汽车选择器,它可以在汽车之间切换并开始与那辆车匹配。那里的一切都有效。唯一的问题是我的“CarCameraScript”有一个变换变量,它始终是 3 辆车中的 1 辆。我希望它根据所选的汽车而改变。

下面看一下 CarCameraScript 的代码

#pragma strict
var car : Transform;
var distance: float = 6.4;
var height: float = 1.4;
var rotationDamping : float = 3.0;
var heightDamping: float = 2.0;
var zoomRatio : float = 0.5;
var DefaultFOV : float = 60;
private var rotationVector : Vector3;

function Start () {

}

function LateUpdate () {
var wantedAngel = rotationVector.y;
var wantedHeight = car.position.y + height;
var myAngel = transform.eulerAngles.y;
var myHeight = transform.position.y;
myAngel = Mathf.LerpAngle(myAngel,wantedAngel,rotationDamping*Time.deltaTime);
myHeight = Mathf.Lerp(myHeight,wantedHeight,heightDamping*Time.deltaTime);
var currentRotation = Quaternion.Euler(0,myAngel,0);
transform.position = car.position;
transform.position -= currentRotation*Vector3.forward*distance;
transform.position.y = myHeight;
transform.LookAt(car);
}
function FixedUpdate () {
var localVilocity = car.InverseTransformDirection(car.rigidbody.velocity);
if (localVilocity.z<-0.5) {
rotationVector.y = car.eulerAngles.y + 180;
} else {
rotationVector.y = car.eulerAngles.y;
}
var acc = car.rigidbody.velocity.magnitude;
camera.fieldOfView = DefaultFOV + acc*zoomRatio;
}

这就是它在侧板上的样子。

http://i.stack.imgur.com/lYJP7.jpg

表示无(变换)的区域是应该根据当前选择的汽车而变化的地方。

现在,这是我的另一个脚本是 CharacterSelectScript

#pragma strict
//this is the currently selected Player. Also the one that will be saved to PlayerPrefs
var selectedPlayer : int = 0;

function Update() 
{ 
if (Input.GetMouseButtonUp (0)) {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;

    if (Physics.Raycast (ray, hit, 100))
        {
                // The pink text is where you would put the name of the object you want to click on (has attached collider).

                 if(hit.collider.name == "Player1") 
            SelectedCharacter1(); //Sends this click down to a function called "SelectedCharacter1(). Which is where all of our stuff happens.

                if(hit.collider.name == "Player2")
                SelectedCharacter2();

                if(hit.collider.name == "Player3")
                SelectedCharacter3();
        } 
        else
        {
        return;               
        }
    } 
}

function SelectedCharacter1() {
    Debug.Log ("Character 1 SELECTED"); //Print out in the Unity console which     character was selected.
    selectedPlayer = 1;
    PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
}

function SelectedCharacter2() {
    Debug.Log ("Character 2 SELECTED");
    selectedPlayer = 2;
    PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
}

function SelectedCharacter3() {
    Debug.Log ("Character 3 SELECTED");
    selectedPlayer = 3;
    PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
}

我将如何获得它以便 SelectedPlayer 更改第一个脚本中的转换?有任何想法吗?

我也有可能不重要的预制脚本。另外,我应该将两个脚本合二为一吗?

OVERALL GOAL: Have the camera change target to the selected car
4

0 回答 0