- Home /
Can't get Object to move Independently
I have two spheres and I want to give them each different locations to move to...no matter how I assign the location01 value, both of my objects move towards it. I plan on creating many objects throughout my game and want to be able to command each object separately to different locations.
// This creates a destination by right clicking the mouse.
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
location01 = hit.point;
}
}
// This moves the objects toward location01.
transform.position = Vector3.MoveTowards(transform.position, location01, speed * Time.deltaTime);
Well, if you have 5 objects in your scene for example, with the same script on it.. .Their all waiting for that right click to move them all to that 1 point...
So, maybe what you want to do, depending on how your game works, is move the right click event to a separate script, and have the object the player has selected, to change their location to that, this way only one thing gets affected at a time with the same script.
Answer by Topthink · Nov 20, 2016 at 09:06 PM
I'm still trying to figure this out. I moved the right click event to a separate script. LoL. I can't get any of them to move with a right click now. I know I'm doing something incorrectly, I just don't know what it is.
I'll keep at it until I figure it out though.