- Home /
Detecting RayCast on moving object?
i have searched alot but could not find any refernce across internet. my case is gameobjects are spawning from the top of screen and translating downwords. each gameObject has trigger and a script that translating it. player have to tap on each object to destroy that specific gameobject. i am using raycasting to detect touch on each object. everything working if speed is normal. when object starts to come fast . the gameobject start ignoring touch. any solution for this? i have to scripts one attach to camera that manage raycasting and other translating tile
Script attached to camera
using UnityEngine;
using System.Collections;
public class RaycastManager: MonoBehaviour {
Touch touch;
public bool stopTime=true;
public float delay,timer;
// Use this for initialization
void Start () {
timer = delay;
}
void Awake()
{
MoveTile.speed = 7f;
}
// Update is called once per frame
void FixedUpdate ()
{
if (Input.touchCount >0) {
//MoveTile.speed = 0f;
touch = Input.GetTouch (0);
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((touch.position)), Vector2.zero);
if (hit.collider.tag == "Pink") {
Destroy (hit.transform.gameObject);
} else if (hit.collider.tag == "Green") {
Destroy (hit.transform.gameObject);
} else if (hit.collider.tag == "Blue") {
Destroy (hit.transform.gameObject);
} else if (hit.collider.tag == "SeaGreen") {
Destroy (hit.transform.gameObject);
} else if (hit.collider.tag == "Purple") {
Destroy (hit.transform.gameObject);
} else if (hit.collider.tag == "Orange") {
Destroy (hit.transform.gameObject);
}
}
}
}
Script attached to each gameobject
using UnityEngine;
using System.Collections;
public class MoveTile : MonoBehaviour {
public float speed;
// Use this for initialization
public void pointerDown ()
{
speed = 0;
gameObject.SetActive (false);
}
// Update is called once per frame
void Update () {
transform.Translate (speed * Vector2.down*Time.deltaTime);
}
}
Answer by giorashc · Jun 07, 2016 at 08:39 AM
Since you are translating your objects in Update()
(instead of FixedUpdate()
) then in high speeds the raycast check point might miss the actual object since Update() might be called several times between two sequential calls to FixedUpdate()
. So while you check the input and raycast in FixedUpdate
the object might already been translated.
Check this link about the differences regarding being in sync with the physics engine.
Try translating your objects in a FixedUpdate()
method instead of in Update()
Also do raycasting checks in Update()
method as FixedUpdate()
is executed in fixed intervals while Update() is called per frame (which on higher fps is more frequent the FixedUpdate()).
giorashc Thanx Alot for your reply. I tried to shift translation into fixed upadate. but face same issue.. i tried another technique my making all gamobject ui image and added event trigger on them and using "on pointer down" i achieve the same target but issues is still same. hight speed cant be handle with both techniques
is unity not capable of handling high speed gameobjects?
It sure does... it just needs to be handled right. Did you try putting logs (Debug.Log(..)) around your updates to understand what exactly happens in which order?
i checked using logs
"'RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((touch.position)), Vector2.zero)""; the hit is racast is not colliding with the collider of gamobject. can say ignoring collision.
When you say 'everything working if speed is normal' you mean that the raycast works (or the event trigger works)?
both things work in normal speed. event triggers and raycast but when speed of gameobject increase the problem arise.
its piano tiles like game and speed of tiles increase with span of time.
Answer by davidjohn123 · Jun 13, 2016 at 06:08 AM
I find solution of this problem after a week of struggle. if someone need solution can contact me.
Hey $$anonymous$$. I am stuck in same situation ,so where to contact you? can you post your email here .
Your answer
Follow this Question
Related Questions
trouble with input touches 1 Answer
From Input.GetMouse to Input.touch. Multi-touch and raycasts? 1 Answer
How to use multi touch 0 Answers
Problem with Input.touchCount 0 Answers