Conversion of screen coordinates and world coordinates + object pooling technology (3D mouse game)

Source: Internet
Author: User
Tags gopher

The game may often encounter the need for an object to move with the mouse, and then need to put the object in a specific mouse position

Implementation method

Camera.main.WorldToScreenPoint

Camera.main.ScreenToWorldPoint

3D hit the ground mouse example

The materials I use here are pretty primitive, almost all made with unity.

First, the hammer.

is two cylinder, in the position of the handle put an empty object used to simulate the action of the Hammer, named Hammer, the hammer as Hammer sub-object, to hammer add animation animation:

Set Hammer's rotation at three keyframe positions, distributed as (45, 0, 0), (-15, 0, 0), (45, 0, 0)

Change the tag of the hammer head to hammer and add a rigid body, tick the usegravity and iskinematic to trigger the hit to the hamster event

And then the Gopher,

This place is replaced with capsule.

Then add animation animation to capsule and place it on an empty object named mouse, change tag to mouse

Add a script for capsule

1  Public classMousecontroller:monobehaviour {2 3     /// <summary>4     ///recycles the mouse.5     /// </summary>6      Public voidRecyclemouse ()7     {8 //Destroy (transform.parent.gameObject);9 Ten         //Object Pool Recycling. One MousePool.Instance.RecycleMouse (transform.parent.gameObject); A     } -  -     //the trigger method to be hit. the     voidOntriggerenter (Collider other) -     { -         if(Other.tag = ="Hammer") -         { +Print"score"); - recyclemouse (); +         } A     } at      -}

The position of the capsule are set at four Keyframe positions (0,-1, 0), (0, 0, 0), (0, 0, 0), (0,-1, 0), respectively.

Drag the mouse to the preset body

Animation state machine for Hammers:

Layout of openings

In order to be able to see the presence of the hamster, I made a simple PNG image with a transparent hole in Photoshop:

<= Here's an invisible picture ...

Create a new material, shader the transparent/diffuse, and drag the picture up there.

Create a new 9 cube, give it the newly created material, change the position and size, adjust the camera, the final result is as follows:

Here's the code on the hammer:

1  Public classHammercontroller:monobehaviour {2     3 Animator Anim;4 Transform head;5 6     voidStart () {7Anim = This. Getcomponent<animator>();8Head = Gameobject.find ("Head"). Transform;9     }Ten      One Vector3 newposition; A Vector3 Screenpos; -  -     voidUpdate () { the mouseshow (); - Hammermove (); -     } -  +     //time-register. -     floatTimer =0; +  A     //the preset body of the gopher. at      PublicGameobject Mouseprefab; -     intRanindexx; -     intranindexy; -  -     //Mouse The way it appears. -     voidmouseshow () in     { -Timer + =Time.deltatime; to         //every over 1s produces a hamster. +         if(Timer >1) -         { theTimer-=1; *Ranindexx = Random.range (0,3); $Ranindexy = Random.range (0,3);Panax Notoginseng             //The gopher is generated from the object pool. -MousePool.Instance.InstantiateMouse (Mouseprefab,NewVector3 (Ranindexx *2,0, Ranindexy *2), quaternion.identity); the         } +     } A  the     //the movement of the hammer and the animation controller. +     voidHammermove () -     { $         if(Input.getmousebuttondown (0)) $         { -Anim. Settrigger ("Attack"); -         } the  -         //converts the coordinate of the hammer to its coordinates on the screen and keeps the y-axis position unchanged.WuyiScreenpos = Camera.main.WorldToScreenPoint (NewVector3 (transform.position.x,1, Transform.position.z)); the  -         //mouse position. WuNewposition =NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, screenpos.z); -  About         //converts the resulting screen coordinates to world coordinates. $Transform.position =Camera.main.ScreenToWorldPoint (newposition); -     } -  -}

Finally, the code for the object pool:

1  Public classMousepool {2 3     //The object pool.4dictionary<string, list<gameobject>> pool =Newdictionary<string, list<gameobject>>();5 6     //Singleton mode.7     Private StaticMousepool instance;8      Public StaticMousepool Instance {9         Get {Ten             if(Instance = =NULL) One             { AInstance =NewMousepool (); -             } -             returninstance; the         } -     } -     PrivateMousepool () {} -  +     //the storage category for the object. -     stringtags; + Gameobject Mouse; A  at     //Object Pool management methods. -      Publicgameobject Instantiatemouse (gameobject prefab, Vector3 position, quaternion rotation) -     { -tags =Prefab.tag; -  -         //if the category exists in the object pool and the number is greater than 0, the object is fetched from the object pool, or an object is instantiated. in         if(Pool. ContainsKey (tags) && pool[tags]. Count >0) -         { toMouse = pool[tags][0]; + Pool[tags]. Remove (mouse); -Mouse.transform.position =position; theMouse.transform.rotation =rotation; *Mouse. SetActive (true); $}Else{Panax Notoginseng             if(!pool. ContainsKey (tags)) -             { thePool. ADD (tags,NewList<gameobject>()); +             } AMouse = Gameobject.instantiate (prefab, position, rotation) asGameobject; the         }         +         returnMouse; -     } $  $     //recycling methods. -      Public voidRecyclemouse (Gameobject mouse) -     { the Pool[mouse.tag]. ADD (mouse); -Mouse. SetActive (false);Wuyi     } the  -}

Okay, the game is almost done.

Web Version Preview

Download the PC version

Conversion of screen coordinates and world coordinates + object pooling technology (3D mouse game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.