- Home /
[illustrated!] [code!] Stop object's movement when it is close to another one
Hey, I was experimenting with a boomerang script and with the help of community here almost got it right. Here is the problem: after throwing the object it returns, but keeps pushing the player as if 2 colliders collided. It shouldn't be this way. I would like it to be the following: after the instantiated object is at the same position of the player or CLOSE to it, it stopped movement. I am attaching the script and illustration (although my game is about mage, I thought batman would illustrate it better).
using UnityEngine;
using System.Collections;
public class boomberang : MonoBehaviour {
public GameObject throwobject;
public bool onetime;
Vector3 oldposition = Camera.main.transform.position;
public float returnspeed = 10;
void Update () {
if (Input.GetButtonDown ("Fire1")) {
StartCoroutine(Throw(35.0f, 10.0f, transform.position, 2.0f));
}
}
IEnumerator Throw(float dist, float width, Vector3 oldposition, float time) {
Quaternion oldRotation = Camera.main.transform.rotation;
float height = Camera.main.transform.position.y;
float timer = 0.0f;
while (timer < time) {
float t = Mathf.PI * timer / time;
float x = width * Mathf.Cos(t);
float z = dist * Mathf.Sin (t);
Vector3 newvector = oldRotation * new Vector3(x,height,z);
if(onetime != true){
throwobject = (GameObject)Instantiate (throwobject, oldposition, transform.rotation);
onetime = true;
}
throwobject.rigidbody.MovePosition(oldposition + newvector);
timer += Time.deltaTime;
yield return null;
}
Debug.Log ("the first coroutine has just ended"); //now let's return the ball
while(throwobject.transform.position != transform.position) {
Debug.Log ("it is not the same");
throwobject.transform.position = Vector3.MoveTowards(throwobject.transform.position, transform.position, Time.deltaTime * returnspeed);
yield return null;
}
}
// 'dist' - distance of the throw.
//'width' - width of the ellipse for the throw.
// 'direction' - vector indicating the direction of the throw
// 'time' - time the throw should take.
}
Here's what I supposed it would be: http://postimg.org/image/jg12mdn9b/
Here's what it runs like: http://postimg.org/image/ia1o1bocf/
Could you help me? If you want to quickly run my script - you can create a Unity FPS Character controller and attach sript to it + create a simple 3d cube obejct. That's what I did. Thanks!
[1]: http://postimg.org/image/jg12mdn9b/
[2]: http://postimg.org/image/ia1o1bocf/
Place them on different layers and go to Physics manager where you can remove the tick in the collision matrix.
$$anonymous$$ake another collider on Player, set to Trigger. Use this to detect when the boomerang has returned.
Thanks for suggestion, I decided not to change collision matrix. Ins$$anonymous$$d I added another collider, this time - it's a trigger. When the ball enters it it dissapears and coroutine stops.
public Collider collider2;
/////////////////////////
void OnTriggerEnter(Collider collider2) {
Destroy(throwobject.gameObject);
}
But! When I want to instantiate it again, it tells me: the ball is destroyed and is missing. Is there a walkaround?
Your answer
Follow this Question
Related Questions
Move object forward 1 unit then stop 2 Answers
How to stop coroutines or functions 2 Answers
Stop moving box when hit other box 1 Answer
Starting a new animation, after stopping another. Without transitions. 1 Answer
how to check movement ended? 1 Answer