///'在Update//(//)函数中加入以下代码来实现player触碰地面后改变地面颜色为红色的功能://n//nc#//nvoid OnCollisionEnter//(Collision collision//)//n{//n if //(collision.gameObject.CompareTag//(///'Ground///'//)//)//n {//n collision.gameObject.GetComponent//<Renderer//>//(//).material.color = Color.red;//n }//n}//n//n//n完整的代码如下://n//nc#//nusing System;//nusing System.Collections;//nusing System.Collections.Generic;//nusing UnityEngine;//n//npublic class PlayerScript : MonoBehaviour//n{//n public Transform follower;//n private Rigidbody _playerRigidbody;//n private Vector3 _controlToGoForward = new Vector3//(0, 0, 10//);//n private Vector3 _controlToGoLeft = new Vector3//(-10, 0, 0//);//n private Vector3 _controlToGoRight = new Vector3//(10, 0, 0//);//n private Vector3 _controlToJump = new Vector3//(0, 5, 0//);//n private Vector3 _controlToGoBack = new Vector3//(0, 0, -10//);//n private bool _isKeySpaceTyped;//n//n void Start//(//)//n {//n _playerRigidbody = GetComponent//<Rigidbody//>//(//);//n }//n//n void Update//(//)//n {//n bool isKeyRTyped = Input.GetKey//(KeyCode.R//);//n bool isKeyTTyped = Input.GetKey//(KeyCode.T//);//n bool isKeyWTyped = Input.GetKey//(KeyCode.W//);//n bool isKeyATyped = Input.GetKey//(KeyCode.A//);//n bool isKeySTyped = Input.GetKey//(KeyCode.S//);//n bool isKeyDTyped = Input.GetKey//(KeyCode.D//);//n bool isKeyShiftTyped = Input.GetKey//(KeyCode.LeftShift//);//n float mouseMoveX = Input.GetAxis//(///'Mouse X///'//);//n float mouseMoveY = Input.GetAxis//(///'Mouse Y///'//);//n float mouseCausedMovedPara = 5;//n _isKeySpaceTyped = Input.GetKey//(KeyCode.Space//);//n//n if //(isKeyWTyped//)//n {//n _playerRigidbody.AddForce//(_controlToGoForward//);//n }//n//n if //(isKeyATyped//)//n {//n _playerRigidbody.AddForce//(_controlToGoLeft//);//n }//n//n if //(isKeySTyped//)//n {//n _playerRigidbody.AddForce//(_controlToGoBack//);//n }//n//n if //(isKeyDTyped//)//n {//n _playerRigidbody.AddForce//(_controlToGoRight//);//n }//n//n if //(_isKeySpaceTyped//)//n {//n _playerRigidbody.AddForce//(_controlToJump//);//n }//n//n if //(isKeyRTyped//)//n {//n transform.position = new Vector3//(-20, 68, -30//);//n }//n//n follower.Rotate//(-mouseMoveY * mouseCausedMovedPara, mouseMoveX * mouseCausedMovedPara, 0//);//n//n if //(isKeyTTyped//)//n {//n follower.Rotate//(-follower.rotation.x, -follower.rotation.y + 1, -follower.rotation.z//);//n }//n }//n//n void OnCollisionEnter//(Collision collision//)//n {//n if //(collision.gameObject.CompareTag//(///'Ground///'//)//)//n {//n collision.gameObject.GetComponent//<Renderer//>//(//).material.color = Color.red;//n }//n }//n}//n//n///

Unity3D教程:实现玩家触碰地面变色的功能

原文地址: https://www.cveoy.top/t/topic/pyrn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录