星幻 发表于 2013-4-2 11:49

Unity3D 力量棒代碼


[*]這是球類遊戲常用的力量棒代碼.. 是轉載自unity3dstudent.com

[*]
//set the maximum width of the bar to be used for the maths function below
var fullWidth : float = 256;

//create a boolean flag we can use to stop and start the choosing of power
var choosing : boolean = false;

//create a private variable (not shown in inspector) to store the current set power
private var thePower : float;

//create a slot to assign my ball prefab to.
var ball : Rigidbody;

//create a slot to assign an empty game object as the point to spawn from
var spawnPos : Transform;

// create a number to multiply the force by as the value of up to 256 may not be enough to
// effectively shoot a ball forward
var shotForce : float = 5;

function Update () {

// detect if key is released and then call the Shoot function, passing the current
// value of 'thePower' variable into it's 'power' argument
if(Input.GetButtonUp("Jump")){
Shoot(thePower);
}

if(!choosing){
//create a power variable and set it to ping pong function
//from current time to 1, and multiply that by a number (or variable holding a number)
thePower = Mathf.PingPong(Time.time, 1) * fullWidth;

//set the width of the GUI Texture equal to that power value
guiTexture.pixelInset.width = thePower;
}
}

// start the 'Shoot' custom function, establish a
// float variable to be fed with a number when function is called
function Shoot(power : float){
//stop the power being changed whilst we shoot a ball by setting choosing boolean to true
choosing = true;

//create a ball, assign the newly created ball to a var called pFab
var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);

//find the forward direction of the object assigned to the spawnPos variable
var fwd : Vector3 = spawnPos.forward;
pFab.AddForce(fwd * power*shotForce);

//pause before resuming the power bar motion
yield WaitForSeconds(2);

//reset choosing to restart the power bar motion
choosing = false;
}

guoer2113 发表于 2017-3-5 19:38

很不错

linhao0824 发表于 2017-3-5 19:35

楼主是超人

yangjingchi 发表于 2017-3-5 19:12

真心顶

youxi02 发表于 2017-3-5 19:50

不错不错

天下 发表于 2017-3-5 19:17

LZ真是人才

huier4536 发表于 2017-7-22 14:45

很不错

liyuan0331 发表于 2017-7-22 14:57

楼主是超人

wpfcool2008 发表于 2017-7-22 15:04

好帖就是要顶

wpfcool2008 发表于 2017-7-22 14:58

难得一见的好帖
页: [1] 2 3 4 5 6
查看完整版本: Unity3D 力量棒代碼