Having problems with OnTriggerEnter()
So I got the player and an enemy. Their inspectors are attached. As you can see the both got a Rigidbody and a Collider. Enemy's collider IsTrigger is checked. The code for the trigger is on my player and is as follows :
public class PlayerTrigger : MonoBehaviour { private Vector3 spawn; %|-948880006_2|% { spawn = transform.position; } %|-278523757_6|% { %|1772643265_8|% if(col.tag == "enemy") { Debug.Log("hey"); Instantiate(deathParticles, transform.position , Quaternion.identity); transform.position = spawn; } } }
The thing is not even the first Debug is showing me anything and I have no idea why.
Just to be sure that nothing is getting in the way, I will give the codes that are attached to my enemy and player. Enemy : Patrol Script :
public class Patrol : MonoBehaviour { %|-1286988724_16|% %|-1962067065_17|% %|-1691085481_18|% void Start () { %|1250423996_20|% %|-230793626_21|% %|-1475356057_22|% %|-409792821_23|% if (transform.position == patrolPoints[currentPoint].position) %|841920102_25|% %|1365858247_26|% %|-1817253691_27|% %|-1894866284_28|% %|-250511739_29|% %|532268223_30|% %|947983162_31|% %|-1251186961_32|% %|2071752502_33|% }
And Player : PlayerMovement2 :
using LockingPolicy = Thalmic.Myo.LockingPolicy; using Pose = Thalmic.Myo.Pose; using UnlockType = Thalmic.Myo.UnlockType; using VibrationType = Thalmic.Myo.VibrationType; // Orient the object to match that of the Myo armband. // Compensate for initial yaw (orientation about the gravity vector) and roll (orientation about // the wearer's arm) by allowing the user to set a reference orientation. // Making the fingers spread pose or pressing the 'r' key resets the reference orientation. public class PlayerMovement2 : MonoBehaviour { private bool sameMovement = false; public float moveSpeed; private float maxSpeed = 15f; public float xGap = 0.3f; public float yGap = 0.6f; private Vector3 input; private Rigidbody rb; private int test; public GameObject deathParticles; // Use this for initialization void Start() { rb = GetComponent<Rigidbody>(); _antiYaw = Quaternion.FromToRotation( new Vector3(myo.transform.forward.x, 0, myo.transform.forward.z), new Vector3(0, 0, 1) ); } // Myo game object to connect with. // This object must have a ThalmicMyo script attached. public GameObject myo = null; // A rotation that compensates for the Myo armband's orientation parallel to the ground, i.e. yaw. // Once set, the direction the Myo armband is facing becomes "forward" within the program. // Set by making the fingers spread pose or pressing "r". private Quaternion _antiYaw = Quaternion.identity; // A reference angle representing how the armband is rotated about the wearer's arm, i.e. roll. // Set by making the fingers spread pose or pressing "r". private float _referenceRoll = 0.0f; // The pose from the last update. This is used to determine if the pose has changed // so that actions are only performed upon making them rather than every frame during // which they are active. private Pose _lastPose = Pose.Unknown; // Update is called once per frame. void Update() { // Access the ThalmicMyo component attached to the Myo object. ThalmicMyo thalmicMyo = myo.GetComponent<ThalmicMyo>(); bool updateReference = false; if (thalmicMyo.pose != _lastPose) { _lastPose = thalmicMyo.pose; if (thalmicMyo.pose == Pose.FingersSpread) { updateReference = true; ExtendUnlockAndNotifyUserAction(thalmicMyo); } } if (Input.GetKeyDown("r")) { updateReference = true; } // Update references. This anchors the joint on-screen such that it faces forward away // from the viewer when the Myo armband is oriented the way it is when these references are taken. if (updateReference) { // _antiYaw represents a rotation of the Myo armband about the Y axis (up) which aligns the forward // vector of the rotation with Z = 1 when the wearer's arm is pointing in the reference direction. _antiYaw = Quaternion.FromToRotation( new Vector3(myo.transform.forward.x, 0, myo.transform.forward.z), new Vector3(0, 0, 1) ); // _referenceRoll represents how many degrees the Myo armband is rotated clockwise // about its forward axis (when looking down the wearer's arm towards their hand) from the reference zero // roll direction. This direction is calculated and explained below. When this reference is // taken, the joint will be rotated about its forward axis such that it faces upwards when // the roll value matches the reference. Vector3 referenceZeroRoll = computeZeroRollVector(myo.transform.forward); _referenceRoll = rollFromZero(referenceZeroRoll, myo.transform.forward, myo.transform.up); } // input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); // if (rb.velocity.magnitude < maxSpeed) // { // rb.AddForce(input * moveSpeed); // } float x = myo.transform.rotation.eulerAngles.x; float y = myo.transform.rotation.eulerAngles.y; float z = myo.transform.rotation.eulerAngles.z; float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); float moveUp = 0; if (15 + xGap < x && x < 180) { moveVertical = -1; } else if (180 < x && x < 345 - xGap) { moveVertical = 1; } if (15 + yGap < y && y < 180) { moveHorizontal = 1; } else if (180 < y && y < 345 - yGap) { moveHorizontal = -1; } if (sameMovement) { moveUp = 30; } Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical); rb.AddForce(movement * moveSpeed); if (transform.position.y < -100) { movement = new Vector3(0, 0, 0); } } float rollFromZero(Vector3 zeroRoll, Vector3 forward, Vector3 up) { // The cosine of the angle between the up vector and the zero roll vector. Since both are // orthogonal to the forward vector, this tells us how far the Myo has been turned around the // forward axis relative to the zero roll vector, but we need to determine separately whether the // Myo has been rolled clockwise or counterclockwise. float cosine = Vector3.Dot(up, zeroRoll); // To determine the sign of the roll, we take the cross product of the up vector and the zero // roll vector. This cross product will either be the same or opposite direction as the forward // vector depending on whether up is clockwise or counter-clockwise from zero roll. // Thus the sign of the dot product of forward and it yields the sign of our roll value. Vector3 cp = Vector3.Cross(up, zeroRoll); float directionCosine = Vector3.Dot(forward, cp); float sign = directionCosine < 0.0f ? 1.0f : -1.0f; // Return the angle of roll (in degrees) from the cosine and the sign. return sign * Mathf.Rad2Deg * Mathf.Acos(cosine); } // Compute a vector that points perpendicular to the forward direction, // minimizing angular distance from world up (positive Y axis). // This represents the direction of no rotation about its forward axis. Vector3 computeZeroRollVector(Vector3 forward) { Vector3 antigravity = Vector3.up; Vector3 m = Vector3.Cross(myo.transform.forward, antigravity); Vector3 roll = Vector3.Cross(m, myo.transform.forward); return roll.normalized; } // Adjust the provided angle to be within a -180 to 180. float normalizeAngle(float angle) { if (angle > 180.0f) { return angle - 360.0f; } if (angle < -180.0f) { return angle + 360.0f; } return angle; } // Extend the unlock if ThalmcHub's locking policy is standard, and notifies the given myo that a user action was // recognized. void ExtendUnlockAndNotifyUserAction(ThalmicMyo myo) { ThalmicHub hub = ThalmicHub.instance; if (hub.lockingPolicy == LockingPolicy.Standard) { myo.Unlock(UnlockType.Timed); } myo.NotifyUserAction(); } void OnGUI() { GUI.BeginGroup(new Rect(10, 10, 300, 500)); GUILayout.Label("Myo X: " + myo.transform.rotation.eulerAngles.x , GUILayout.MinWidth(300), GUILayout.MinHeight(30)); GUILayout.Label("Myo Z: " + myo.transform.rotation.eulerAngles.z , GUILayout.MinWidth(300), GUILayout.MinHeight(30)); GUILayout.Label("Myo Y: " + myo.transform.rotation.eulerAngles.y , GUILayout.MinWidth(300), GUILayout.MinHeight(30)); GUI.EndGroup(); } }
The code for movement is using the Myo Armband so it might be a bit confusing. Hopefully that aint the problem. Anyway, all help appreciated.
Answer by Batonikos_ · Sep 25, 2016 at 05:15 PM
I'm sorry for some weird reason, where there should be a line break there is this stupid thing %|-1286988724_16|% . No idea why... Hope people can still help even with this.
Your answer
Follow this Question
Related Questions
Destroy object on hover? 1 Answer
Moving an Object Using a Trigger 0 Answers
Collider dosent detect trigger 0 Answers
OnTriggerEnter and OnTriggerExit called twice despite checking it 1 Answer