How to make my moving object (in game) stop once it collides
hi
I have a problem. Im creating a game and basically i have 2 boxes. if these boxes loook each other from the side that i define (i put a plane to their surfaces) then a button appears.. If i click to this button one piece moves to another until it collides and it doesnt stop until it reaches to the middle of the other cube..
Im detecting hits (raycasting) from my first plane to another object plane.. IF hit happens then i add them to an array. If there are 2 objects in array button shows up... I tried many things including deactivating script but then there are other problems shows up.. Like i cannot use raycasting again cuz i deactivate it...
Here is my code:
//This is how i move from one object to another
Transform a = gameTag.gameO[0].transform;
Transform b = gameTag.gameO[1].transform;
a.position = Vector3.MoveTowards(a.position,b.position, moveSpeed * Time.deltaTime);
//this is my raycast system. Im putting this script to each of my objects..
RaycastHit hit;
if (flagTrigger==false)
{
if (Physics.Raycast(transform.position, -transform.forward, out hit, 10))
{
var forward = transform.TransformDirection(Vector3.forward) * 10;
arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
if (hit.transform.tag == "Select")
{
if (!gameTag.gameO.Contains(this.gameObject))
gameTag.gameO.Add(this.gameObject);
}
else
{
gameTag.gameO.Remove(this.gameObject);
}
}
//Lastly here my kind of trigger system
void OnTriggerEnter(Collider other)
{
Raycast u = other.GetComponent<Raycast>();
if (u != null)
{
arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
if (enabled && gameTag.gameO.Contains(this.gameObject))
{
gameTag.gameO.Remove(this.gameObject);
flagTrigger = true;
//enabled = false;
}
}
}
I commented out the enabled part for now.. If i use it i cannot use the same script again... I know the problems but i donno the answer for it.. Any help please..
Answer by AMU4u · Feb 25, 2017 at 04:28 PM
You are over thinking it, is why.
Change your movement code from moveTowards to the objects rigidbody and change it's velocity. If it hits another object, regardless of forces acting in script, it won't move passed another rigidbody on it's collision layer (or another collision layer it is able to interact with).
rb.velocity = new Vector3(someDirection.x * someSpeed * Time.deltaTime, 0f, rb.velocity.z);
This will be you're new script. You wont need OnCollisionEnter anymore.
If you want to move towards the other cube by the button, go;
void GetTargetPos()
{
Vector3 target = new Vector3(targetPos.x, 0f, targetPos.z);
}
void FixedUpdate()
{
GetTargetPos();
rb.velocity = new Vector3(targetPos.x * someSpeed * Time.deltaTime, 0f, targetPos.z * someSpeed * Time.deltaTime);
}
You could even make this neater by putting the V3 velocity method in it's own function outside of your FixedUpdate command center.
void GetTargetPos()
{
Vector3 target = new Vector3(targetPos.x, 0f, targetPos.z);
}
void $$anonymous$$oveTowardTargetByTime()
{
rb.velocity = new Vector3(targetPos.x * someSpeed * Time.deltaTime, 0f, targetPos.z * someSpeed * Time.deltaTime);
}
void FixedUpdate()
{
GetTargetPos();
$$anonymous$$oveTowardTargetByTime();
}
That way you keep your update functions nice and easy to unit test.