Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NickolasTheGreek · Mar 11, 2012 at 11:29 AM · movegridcubeblock

Can't figure out how to achieve an effect like QUBE

I saw this game recently and really liked it and I thought I could try porting it to Unity3D. http://www.youtube.com/watch?v=EoS2rSLOr_0

I can't figure out how to move the cubes to certain positions and not anywhere. Could you provide a C# script for it? Thanks in advance :)

P.S. I know how to implement the blue blocks using rigidbody.AddForce. I want to know the implementation of the red blocks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Mar 11, 2012 at 01:37 PM

Usually i don't write whole scripts for other people that are too lazy... In this case it's actually so simple that it took only a few min.

 public class RedCube : MonoBehaviour
 {
     Collider m_Collider;
     Transform m_Transform;
 
     public float currentTarget = 0;
     public float max = 3;
     public float Speed = 1;
 
     public float current = 0;
     private float m_Offset;
     void Start ()
     {
         m_Collider = collider;
         m_Transform = transform;
         m_Offset = m_Transform.localPosition.x;
     }
 
     bool CheckClick()
     {
         Ray R = Camera.main.ScreenPointToRay(Input.mousePosition);   // mouse point
         // Ray R = Camera.main.ViewportPointToRay(Vector2.one*0.5f);   // screen center
         RaycastHit hit;
         return m_Collider.Raycast(R,out hit,10000);
     }
     
     void Update ()
     {
         if (Input.GetMouseButtonDown(0) && CheckClick())
             currentTarget = Mathf.Min(currentTarget+1,max);
         if (Input.GetMouseButtonDown(1) && CheckClick())
             currentTarget = Mathf.Max(currentTarget-1,0);
         
         if (current != currentTarget)
         {            
             current = Mathf.MoveTowards(current,currentTarget,Time.deltaTime*Speed);
             if (Mathf.Abs(current - currentTarget) < 0.01f)
             {
                 current = currentTarget;
             }
             Vector3 pos = m_Transform.localPosition;
             pos.x = m_Offset + current;
             m_Transform.localPosition = pos;
         }
     }
 }

I've tested the script without any FPS controller. Just with a fix camera so i changed the raycast to mouseposition instead of screencenter.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 11, 2012 at 01:56 PM 0
Share

Here's the example with unitypackage

avatar image Bunny83 · Mar 11, 2012 at 02:13 PM 0
Share

Don't forget to add a kinematic rigidbody to the cube if it should be able to push other things. Also you might have to implement some manual collision checks if two of these cubes can be pushed into the same space.

avatar image NickolasTheGreek · Mar 11, 2012 at 03:13 PM 0
Share

I am not home right now but I'll check the script ASAP. Thanks a lot :)

avatar image NickolasTheGreek · Mar 11, 2012 at 03:56 PM 0
Share

Works like a charm :) Thanks a lot. Could you please explain this line: // Ray R = Camera.main.ViewportPointToRay(Vector2.one*0.5f);

I don't get it...

avatar image Bunny83 · Mar 11, 2012 at 04:55 PM 0
Share

$$anonymous$$y test project uses the mouse position. So it raycasts whereever you click with your mouse.

In QUBE you have a first-person-view and you usually aim with the some kind of crosshair at the screen center. Viewport coordinates goes from 0 to 1 (left to right or bottom to top). The screen center is at (0.5f, 0.5f). So i used the constant "one" which is the same as new Vector2(1,1) and multiplied it with 0.5 so it gets new Vector2(0.5f, 0.5f).

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Snap blocks that aren't cubes. 1 Answer

Performance While Loading Map 2 Answers

How do I make a block move to another blocks position 1 Answer

Pushable Blocks 3 Answers

Generating a grid of cubes with space between. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges