欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌。这里介绍的是如何使用脚本最简单的模拟出跳跃的效果。脚本源码如下:var speed = 3.0; //This data type is a float.var jumpSpeed = .0;var grounded = true;function Update () { var x : Vector3 = Input.GetAxis("Horizontal") * transform.right * Time.deltaTime * speed; var z : Vector3 = Input.GetAxis("Vertical") * transform.forward * Time.deltaTime * speed; //transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.Translate(x z); transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up); if(Input.GetButtonDown("Jump")) { Jump (); }}function Jump (){ if(grounded == true) { rigidbody.AddForce(Vector3.up * jumpSpeed); grounded = false; }}function OnCollisionEnter(hit : Collision){ grounded = true; Debug.Log("I'm colliding with something!");}其中,这行代码尤为重要:1transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up);如果注释掉这行代码,物体在跳跃的时候会出现空中翻转的现象,添加后物体不会出现除了z轴之外的其他旋转。一个完善的角色移动的脚本源码如下:var speed : float = 6.0;var jumpSpeed : float = 8.0;var gravity : float = .0;private var moveDirection : Vector3 = Vector3.zero;function Update(){var controller : CharacterController = GetComponent(CharacterController);if(controller.isGrounded){ moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Allows for player input moveDirection = transform.TransformDirection(moveDirection); //How to move moveDirection *= speed; //How fast to move if(Input.GetButton("Jump")) { moveDirection.y = jumpSpeed; }}//Apply gravitymoveDirection.y -= gravity * Time.deltaTime;//Move the controllercontroller.Move(moveDirection * Time.deltaTime); }如果想添加动画的话,使用如下代码即可:function Update(){ if(Input.GetKey("d") || Input.GetKey("right")) { animation.Play("RunFwd"); } else if(Input.GetKey("a") || Input.GetKey("left")) { animation.Play("RunBkwd"); } else if(Input.GetKey("w") || Input.GetKey("up")) { animation.Play("StrafeL"); } else if(Input.GetKey("s") || Input.GetKey("down")) { animation.Play("StrafeR"); } else { animation.CrossFade("Idle"); }}更多精彩请点击
推荐整理分享Unity3D如何使用脚本实现跳跃的效果(unity3ds),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity3d的,unity3d的,unity3d documentation,unity 3d教程,unity3ds,unity3d基础操作,unity3ds,unity3d documentation,内容如对您有帮助,希望把文章链接给更多的朋友!
unity3d游戏开发之如何快速接入渠道SDK 欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d
Unity3D游戏开发最佳实践技巧(三) 欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d
Unity 3D游戏开发引擎:最火的插件推荐 摘要:为了帮助使用Unity引擎的开发者制作更完美的游戏,我们精心挑选了十款Unity相关开发插件和工具。它们是:2DToolkit、NGUI、Playmaker、EasyTouchEasyJoyst