找回密码
 立即注册
查看: 4361|回复: 55

Unity3D 力量棒代碼

[复制链接]
发表于 2013-4-2 11:49 | 显示全部楼层 |阅读模式
  • 這是球類遊戲常用的力量棒代碼.. 是轉載自unity3dstudent.com

    1. //set the maximum width of the bar to be used for the maths function below
    2. var fullWidth : float = 256;

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

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

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

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

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

    14. function Update () {

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

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

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

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

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

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

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

    40. //reset choosing to restart the power bar motion
    41. choosing = false;
    42. }
    复制代码

发表于 2017-3-5 19:38 | 显示全部楼层
很不错
发表于 2017-3-5 19:35 | 显示全部楼层
楼主是超人
发表于 2017-3-5 19:12 | 显示全部楼层
真心顶
发表于 2017-3-5 19:50 | 显示全部楼层
不错不错
发表于 2017-3-5 19:17 | 显示全部楼层
LZ真是人才
发表于 2017-7-22 14:45 | 显示全部楼层
很不错
发表于 2017-7-22 14:57 | 显示全部楼层
楼主是超人
发表于 2017-7-22 15:04 | 显示全部楼层
好帖就是要顶
发表于 2017-7-22 14:58 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 20:57 , Processed in 0.106621 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表