- Home /
Speed/trigger only working once
I have a spaceship that contains a cube, when that cube is collided with by the hand (I have a leap motion hand tracker) it is supposed to add one "speed". The button and the hand both have box colliders set to trigger. I have a script on the cube that links to the script on the hand.
Hand Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spacemove : MonoBehaviour {
public int speed;
public Rigidbody rb;
// Use this for initialization
void Start () {
rb.MovePosition(transform.position + transform.forward * speed);
}
// Update is called once per frame
void Update () {
rb.MovePosition(transform.position + transform.forward * speed);
}
public void newfunction (){
speed = speed + 1;
Debug.Log ("test");
return;
}
}
Cube Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class accel : MonoBehaviour {
public spacemove other;
public bool check;
void Start () {
check = false;
}
void Update () {
check = false;
}
public void OnTriggerEnter(Collider box) {
if (CompareTag ("flytrigger")) {
check = true;
if (check = true) {
other.newfunction ();
Debug.Log ("test on accel");
check = false;
}
}
}
}
This works fine for the most part the first time the hand collides with the cube. The second time nothing happens. I don't get the debug log showing that the trigger worked or that speed was changed (I made sure collapse was off so I could see) and the speed variable does not change. Some times seemingly randomly the first collision triggers it a random number of times but I think I fixed this with check = false at the end of my if(check = true) statement
Your answer
Follow this Question
Related Questions
Trigger Colission not only works sometimes 1 Answer
Box mechanic: Is it possible to know when the box is closed on all margins? 1 Answer
get only one colliding body 3 Answers
How To: One collider for detection; one for collision. 1 Answer
Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer