//Note whether the name of the move is the same as the name of your new script Public classmove:monobehaviour{//like a component-oriented interface, if it is public, you can see a property called Go in the component PrivateGameobject go; //Initialization Program voidStart () {//find the Game object named Cube4Go = Gameobject.find ("Cube4"); //set the color of the game object in the render componentGo.renderer.material.color =color.red; } //The game runs one frame at a time, and each frame calls the update () method voidUpdate () {//Check the keyboard at all times in each frame if(Input.getkey (keycode.a)) {//Time.deltatime is the unit of the frame, here is 5 units//determine the upper and lower left and right of X, y by viewing the coordinate system//space.self Self-coordinate systemGo.transform.Translate (0,0,5*Time.deltatime, space.self); } if(Input.getkey (KEYCODE.D)) {go.transform.Translate (0,0, -5*Time.deltatime, space.self); } if(Input.getkey (KEYCODE.S)) {go.transform.Translate (-5*time.deltatime,0,0, space.self); } if(Input.getkey (KEYCODE.W)) {go.transform.Translate (5*time.deltatime,0,0, space.self); } }} Pull the script into the camera and see in the Inspector At this time, run the game, can be moved up and down through the w,a,s,d, but this game object bump together back overlap, we want the effect is can collide. |
The code is as follows Public classPzjc:monobehaviour {//Use this for initialization voidStart () {}//Update is called once per frame voidUpdate () {}/// <summary> ///This method will be executed every time the game object collides/// </summary> /// <param name= "CO" >can get a hit game object</param> voidOncollisionenter (Collision co) {Co.gameObject.renderer.material.color=Color.Blue; }} Because of the Cube4 of the operation, then pull the script to Cube4 Now run the game, action Because CUBE4 first collided with plane, all plane will change color. |