简体   繁体   中英

Any ideas on improving my item placement system in Unity C#?

This is my system:

  1. On item selected from inventory --> Ghost (semi-transparent) object is instantiated and positioned and rotated according to the player and terrain

  2. OnClick --> Ghost is destroyed and prefab is added in its place.

This system works very well and looks great... The only problem is players have to carefully maneuver to get the walls etc straight and aligned with other walls. The system is very similar to Rust. I would like players to still place anything where they want but: I want the system to to align walls when the ghost gets held adjacent to another wall and a roof over 4 walls to be automatically positioned...

I have no problem with implementing any system (I know C# very well, professionally). I just want some ideas on what the best way is to do this?

My idea was to add a box collider (as a trigger) to object and use OnTriggerEnter to change the position. Is there a better/cleaner/easier way?

This way you suggested is very common. Kerbal Space Program uses these "Mounting Points" to attach parts correctly.

I would implement a mounting point system with tags on a generic basis so you can easily create new objects with this functionality. Like you have a mounting ball (prefab?) this ball always has a parent which it belongs to and a trigger, that maybe is based on the parents's scale. Then this ball has a tag to filter out any other mount balls that should not snap to this (ex. tire + wall = stupid).

When a different ball with the same tag collides with the first one, you should decide what snaps to what based on the parents state (ex. wall that is already placed is likely to have a physic engine suspended state, the other "hold" by the player will not). After this you simply change the 2nd wall's ball to the 1st wall's ball transform and you're done.

If you keep this generic enough you can easily decorate any object with this mounting balls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM