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 Tristsin · Feb 25, 2013 at 06:47 AM · movementbeginnergrid

beginner programming woe grids

What I'm currently trying to accomplish is a basic script for movement on a grid.

What I've done to begin with: Created a 11 by 11 grid of "Tiles" which are simply GameObject cubes. I have a script to change the color to blue when I mouse over a cube, and to return to it's original color afterwards attached to each cube(I did so by using a Prefab to make it much more simple). This script works fine, no problems. The problem lies with my next script, meant to create a movement to whichever cube I click on.

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {    
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     }
         
     void OnMouseUp() {
         Debug.Log("Clicked?");
         iTween.MoveTo(GameObject.FindGameObjectWithTag("Player"), new Vector3(transform.position.x, transform.position.y, transform.position.z), 2f);
     }
     
 }


This is the current script. It's basic. I simply want it to move to whichever cube I click on. It works, in that my Player Character(which right now is simply another cube) moves to the cube I click it, it's just the problem is that it moves downwards into the cube I click on so I end up with only about half a cube sticking up when what I want is entire cube to rest on top of the other cube I'm using for a board.

I half-way resolved the issue by doing the transform.position.y times 1.33, which put the cube on top of the other cube, or at least appearing to be. The problem with that is, if I ever want to raise the up higher than simply that flat terrain, it rests above the terrain(even with gravity enabled..).

The problem APPEARS to be that the cube is moving based on it's center, which is the very center of the cube and it moves the center of my cube to where I clicked the mouse. How can I fix this?

Sorry if I didn't explain myself well, I'm rather new to Unity, as you can probably tell.

Comment
Add comment · Show 4
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 AlucardJay · Feb 25, 2013 at 06:50 AM 0
Share

EDIT LINE 16 , this is NOT SUITABLE for Unity Answers.

avatar image Tristsin · Feb 25, 2013 at 06:53 AM 0
Share

Edited! So sorry, completely forgot about that.

avatar image Fattie · Feb 25, 2013 at 07:27 AM 0
Share

also normally if you have meaningless subject lines the question will just be instantly deleted by moderators, and you'll have to type it all in again

avatar image Tristsin · Feb 25, 2013 at 07:32 AM 0
Share

For reference, what would be a good subject line for this particular problem? Sorry.

1 Reply

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

Answer by fafase · Feb 25, 2013 at 06:53 AM

Yep, as you move with transform, the collision is ignored, or you would have to do it by code.

Now, either

  1. you modify the y value as you did with half of the cube and half of your guy

  2. or use a Character controller which will solve the going down issue

  3. or you place an empty game object at "ground level" on each of the cube and you use this one as target instead of the cube position. Good point about that one, you can create a prefab and whenever you add a cube it has the target attached to it regardless of its height on the world

P.S: Your debug is...unconventional

Comment
Add comment · Show 5 · 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 nsxdavid · Feb 25, 2013 at 07:00 AM 2
Share

Here is an example of using another object to offset the pivot of a cube:

Cube Offset

avatar image Tristsin · Feb 25, 2013 at 07:08 AM 0
Share

I attempted to add a character controller to my Player Character, but it still sunk into the half-way into the terrain. $$anonymous$$aybe I had something off with the Rigidbody? Not sure what it could have been

Not entirely sure what you mean about the empty game object. I see nsxdavid's picture, with the Cube as the child of the Character, but where exactly do I place the empty game object? You said at ground-level, which to me implies the bottom of the cube, but in the picture he has it at 0.5, which puts the empty game object at the top of the cube.

Also, how exactly would I use the empty game object as the target rather than the cube's position?

Also, I clearly sound like a complete idiot, but I don't understand what you mean when you say "modify the y value as you did with half of the cube and half of your guy". Greatly appreciate your help!

P.S: I meant to edit that out to begin with XD. I try to keep things lively.... I guess you could say

avatar image AlucardJay · Feb 25, 2013 at 07:21 AM 0
Share
  • Create an empty game object, position it at 0, 0, 0

  • Create a cube, position it to 0, 0.5, 0

  • drag the cube into the empty, making the cube a child of the empty

  • now if you place the empty at your desired position, the bottom of the cube will be at the specified y-value.

Using an empty gameObject is a common way to define the pivot of your object. The child stays in relation to the parent, so basically if you place the empty at 10, 5, 10 then the cube will be at 10, 5.5, 10. Did I just make it more confusing?!

avatar image nsxdavid · Feb 25, 2013 at 07:27 AM 0
Share

So now you use that empty game object (what I named "Character" in my example) as the thing you move around. The cube is 'resting' on top of it (aka the .5 offset in Y) so it'll be... as we say in the biz... well grounded. :)

avatar image Tristsin · Feb 25, 2013 at 07:29 AM 0
Share

No, you didn't make it more confusing. In fact, you explained it perfectly. I have it working now, thank you.

Also, thank you nsxdavid, the picture was really helpful especially with alucardj's explanation I ended up understanding perfectly by using both. Big thanks to all of you guys, alucard, david, and fafase

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[Answered]Rigid body Movement C# 0 Answers

Problem with collecting plants 0 Answers

Moving the camera with mouse not working 1 Answer

Help with Basic Movement Script 1 Answer


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