- Home /
How do I get a single object to move?
I have multiple spheres and I want to move them independently. I want to "left-click-mouse" to select the sphere I want to move. Then I want to "right-click-mouse" to select a location for the sphere to move to. In theory, I should (I want to...) be able to move ALL the spheres simultaneously but each to a different location. I'm not getting any errors...but I'm not getting any movement either. Any suggestions?
void Update()
{
///// I select the object here...
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Collider col = hit.collider;
if (col != null)
{
test01 = hit.collider.name;
Debug.Log (hit.collider.name);
}
}
}
///// I select the movement destination here...
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
goToHere = hit.point;
Debug.Log (hit.point);
}
}
///// I (attempt) to get the object moving here by using foreach to check through every object and move the one with correct name
foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
{
if (gameObj.name == test01)
{
transform.position = Vector3.MoveTowards (transform.position, goToHere, speed * Time.deltaTime);
}
}
}
Answer by christoph_r · Nov 23, 2016 at 08:59 PM
Calling FindObjectsOfType is very slow and should be avoided in Update().
You are actually moving the GameObject with the movement script. You need to specify the GameObject or Transform that you want to move, otherwise transform is a reference to the transform of the GameObject your script is attached to.
Instead of referencing by name, why not reference the game object directly? Simply create a variable of type GameObject or Transform. You can then assign it through:
myGameObject = hit.collider.gameObject;
in line 16 in your snippet.
Thank you for your 3 comments.
Paragraph one: $$anonymous$$y RTS won't have much in it in terms of objects so I don't know if being "slow" will really hurt things. Thank you though.
Paragraph two: I don't really understand what you are saying here. I really appreciate your help...I just don't fully understand what you are saying. I apologize.
Paragraph three: $$anonymous$$y impression is this will be very helpful. I will try it immediately. Thanks.
Again, thank you for taking the time to help me.
There's hardly ever a reason to constantly search all objects, it's usually a good idea to simply store references. Even if it works fine in your current game, it's just one of those things you shouldn't make a habit of. Eventually you'll end up calling it on multiple objects and your scene grows and the performance overhead becomes more and more noticeable etc.
Regarding Game Object references: Every GameObject in your scene has a Transform attached to it. The GameObject class keeps a reference to its transform in a field handily called transform
. In order to move a specific object, you must of course call the methods and change the fields of that object's transforms. To do that, you need a reference to that transform, or in other words a member variable of the type transform (or game object, and then use myGameObject.transform
). Changing fields of just transform
will move and rotate the GameObject your script is attached to, as it is basically the same reference as gameObject.transform.
Your explanations are excellent and I can tell it took you some effort to share all that. I'll exa$$anonymous$$e carefully everything you mentioned and I'm sure it will help me produce a better program.
I appreciate the time you gave me to help me understand Unity a little better.
Your answer
Follow this Question
Related Questions
Character Controller Follow me in the air 1 Answer
How can I move an object physically correct around an other object? 1 Answer
Moving objects using raycasting? 1 Answer
Ignore Collider 0 Answers