0

edit:player, coin ,magnet,magnet collectible 4 different game objects

when i collide with magnet collectible, magnet gameobject gets activated.it has a big collider and no mesh so it collides before player does.the idea is when it collides with coins , coins should move towards the player or even magnet would work since its position is same with players because of the attached script.

pics:

pic1

2

3

4

i am trying to make 3d runner game,i have read all the related threads and watched everything thats on youtube.this might sound like a cheesy problem to you but i am a beginner at coding. so my problem is that after i pickup the coin magnet i set magnet gameobject active and when it collides with gold, gold should move towards to my player. i couldnt make this work. been trying for a day and a half now.waiting for your solutions also i tried codes by putting debug.log in there and they got printed to console.seems like coins wont change their position unless you guys give it a go

i attach this to coins

     public   GameObject p;
private float movementSpeed= 10f;
private void Start()
{
   

}

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Magnet"))
    {
        
        transform.position = Vector3.MoveTowards(transform.position, other.transform.position, movementSpeed * Time.deltaTime);

    }
}

and this to magnet

void Start()
{
    transform.position = p.transform.position;
}

// Update is called once per frame
void Update()
{
    transform.position = p.transform.position;
}

also tried this code for coin but it didnt work aswell

public   GameObject p;
private float movementspeed= 10f;
private void Start()
{
   

}

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Magnet"))
    {
        
        transform.position =(transform.position - p.transform.position)*time.deltatime*speed;

    }
}

if anything else is needed i will update the post immediatly.i am only here f5ing the page waiting for solutions. thanks

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
eggs
  • 1
  • 1
  • Hi and welcome to stack overflow. Debugging questions need to include all steps necessary to reproduce the problem. See [ask] for more information. This question should be edited to describe the hierarchy of gameobjects and also which components are attached to each gameobject. Screenshots are fine for this but detailed text is fine if you prefer. – Ruzihm Feb 02 '21 at 21:06
  • 1
    Does this answer your question? [Collision detection not working unity](https://stackoverflow.com/questions/43939179/collision-detection-not-working-unity) Be sure to read the **Missing RigidBody** section – Ruzihm Feb 02 '21 at 22:44
  • @Ruzihm i read it all it doesnt even relate to my problem please dont lock the tread – eggs Feb 02 '21 at 23:22
  • for real it is similar but not the same problem it doesnnt really relate – eggs Feb 02 '21 at 23:23
  • The unity documentation [informs you](https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html) that for two objects to cause a call to `OnTriggerEnter`, "Both GameObjects must contain a Collider component. **One must have Collider.isTrigger enabled, and contain a Rigidbody**" This is exactly what the linked post is discussing, when it tells you that one object must have a rigidbody and that "OnTrigger" is available to prevent interaction with an object's velocity. – Ruzihm Feb 02 '21 at 23:26
  • Another possible problem is that both your magnet and your coins have isTrigger = true, and as the unity documentation explains, "**If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.**" However, the question does not show or explain the configuration of the attached colliders, so it's impossible to know for sure if this is relevant or not... See [mre] for more information. – Ruzihm Feb 02 '21 at 23:30

0 Answers0