- Home /
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.
EDIT LINE 16 , this is NOT SUITABLE for Unity Answers.
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
For reference, what would be a good subject line for this particular problem? Sorry.
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
you modify the y value as you did with half of the cube and half of your guy
or use a Character controller which will solve the going down issue
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
Here is an example of using another object to offset the pivot of a cube:
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
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?!
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. :)
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
Follow this Question
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