- Home /
issue with making a moving object trap c# (SOLVED)
What I am trying to do is have the player "step" on an object (trigger) and have that object call a script that moves an object(s) from Point 1 to Point 2. And if the player hits the collider of one of the moving objects, it restarts the level.
So there are 3 scripts - I think 3 is needed, but if anyone knows how to just make it one, that would be awesome.
This is the script for the object that moves from point 1 to point 2 (this seems to be working)
using UnityEngine;
using System.Collections;
public class Trap : MonoBehaviour
{
public Vector3 point2;
IEnumerator Start ()
{
Vector3 point1 = transform.position;
while (true)
{
yield return StartCoroutine(MoveObject(transform, point1, point2, 3.0));
yield return StartCoroutine(MoveObject(transform, point2, point1, 3.0));
}
}
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
}
This is the code for the collider that triggers the moving object (this does not work, not sure why)
using UnityEngine;
using System.Collections;
public class TrapTrigger : MonoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.tag == "Player")
{
MyScript myScript = other.gameObject.GetComponent<MyScript>();
myScript.enabled = true;
}
}
}
And here is the script for the restart function if the player enters the collider (trigger) of the moving object. (this isn't working either - might be because of the moving script)
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ColRestart : MonoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.tag == "Player")
{
SceneManager.LoadScene(“Level_03”);
}
}
}
any help is very much welcomed, Thank you
The way you initialize movement for Trap
is wrong: Start()
will start 2 new coroutines every frame.
Just make Start()
regular function (with return type void
), and remove the while
loop from it, it will work a lot faster and cleaner.
so i would make the start function this?
void Start ()
{
Vector3 point1 = transform.position;
}
and thats it?
I would have to confirm this, but I don't agree. there is yield return StartCoroutine
. So until first coroutine won't finish, second won't start. And that's ok for me
You are right, I misunderstood what will happen: I've just checked with this exact code, and it does wait for the coroutine to finish, then starts the other one, etc.
@bunnynsnake, ignore my comment.
Answer by DawidNorasDev · Jun 20, 2018 at 07:56 AM
The common problem to this is, do you use 2D Colliders? If that's the case, the method is: OnTriggerEnter2D(Collider2D collider)
And as @Harinezumi posted in comment, if you are using 3D colliders, the method is: OnTriggerEnter(Collider collider)
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Exactly, the parameter to OnTriggerEnter()
in TrapTrigger
and ColRestart
is wrong, it needs to be Collider...
, not Collision...
, a common issue to 2D and 3D as well.
You need Collision
or Collision2D
for OnCollision...()
functions ( Enter
, Stay
, Exit
, and its 2D versions), which are used for non-trigger colliders. It gives you a bit more information, like contact points. Check out the $$anonymous$$essages part of Collider
component in the scripting reference: https://docs.unity3d.com/ScriptReference/Collider.html
thank you, the TrapTrigger is showing the player is colliding now, but it is not activating the script to move the object
If you have an object with 2D colliders, you need to use 2D
versions of those functions. (they have 2D
suffix to them)
If you have an object with 3D colliders (known just as "colliders"), you need to use 3D
versions of those functions (they do not have any suffix to them)
Example: OnTriggerEnter2D(Collider2D collider)
For 2D Collision system using Collider2D components OnTriggerEnter(Collider collider)
For 3D Collision system using Collider components
perhaps there is an easier way to move an object back and forth, from one point to another? I would still want to keep the collision the restart function with the object that is moving.
Answer by kyle-misner-kriel · Jun 19, 2018 at 10:43 PM
Is OnTriggerEnter() getting called for either one of these scripts, and what is the value of of their "other.tag" property?
They are getting called now that i switched it from Collision... to Collider... but is is still not having the desired result. the value of the tag is represented by the player if i understand you question, the please is meant to be what is interacting with everything. When i do a debug, it shows the player go though the collider "TrapTrigger" but it does not call the script to move the objects. Also, i have another issue where i want the objects to be inactive for the restart function until they start to move, but one thing at a time.
I suppose you want to activate Trap when player goes in range (hits trigger)
But you enable the script that is on the player other.gameObject.GetComponent<$$anonymous$$yScript>();
called $$anonymous$$yScript. You do not enable the Trap at all. I think what you have is Trap
script and TrapTrigger
on one object. so in TrapTrigger use GetComponent<Trap>()
and enable it. TIP: use CompareTag(string tag)
function when comparing tags. https://docs.unity3d.com/ScriptReference/Component.CompareTag.html
Yes, I do want that. The "TrapTrigger" script is on a game object in the floor. the "Trap" script is attached to a different game object.
I don't really want them on the same object, that wouldn't really work.