- Home /
Raycast Delay
Hi, I am trying to make a game which is similar to fruit ninja, in which u swipe to cut something. I am right now making it on the mac with the mouse(bcos i dont have iOS license). So what i do is i send a ray from the mouse position on screen whenever the user holds the mouse button and check with the 3dobjects(which are moving). So when i do a swipe with the mouse, sometimes it doesnt work. It only works when i move the mouse slowly. If i swipe very fast, most of the time the ray collides with nothing. I dont think it is because of the mac's performance because its an iMac. I have tried putting on FixedUpdate(), LateUpdate() nothing worked. I get an fps of 1500. I just want to know whether there is any other way i can check my collision or change my code in someway so the raycast is more faster. I also want to know whether this will become worse on the iPhone, because iphone is less powerful compare to iMac.
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetKey(KeyCode.Mouse0))
{
trail.time = 0.2;
if(Physics.Raycast(ray,hit,100)){
if((Mathf.Abs(e.delta.x)+Mathf.Abs(e.delta.y))>0)//for checking drag
{
hit2 = hit;
cubes = hit2.collider.gameObject;
cubes.SendMessage("Slashed");
}
}
}
Answer by Fr0sZ · Jul 18, 2011 at 02:55 PM
Try checking if there is a object between the last 2 rays
Answer by drandx · Sep 16, 2011 at 10:12 PM
Hi I want to know if somebody has solved this, I'm having the same problem with the raycast delay, and i have spent a long time trying to get a solution.
Next, this is my script. This scripts Drag Objects, correctly, but when the movement is too fast, it seems like the raycast doesn't work.
private var targetItem : GameObject;
private var scrollDistanceX : float; private var scrollDistanceY : float;
var horizontalLimit : float = 2.9; var verticalLimit : float = 4.40;
function FixedUpdate() { if (Input.touchCount >0) {
// finger data
var f0 = Input.touches[0];
// finger delta
var f0Delta : Vector3 = new Vector3(f0.deltaPosition.x, -f0.deltaPosition.y, 0);
//Ray Cast Info
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(f0.position);
if(Physics.Raycast(ray,hit,100))
{
targetItem = hit.transform.gameObject;
if(f0.phase == TouchPhase.Moved)
{
scrollDistanceX = targetItem.transform.position.x;
scrollDistanceY = targetItem.transform.position.y;
var scrollDeltaX = f0.deltaPosition.x;
var scrollDeltaY = f0.deltaPosition.y;
scrollDistanceX = Mathf.Clamp(scrollDistanceX+scrollDeltaX*Time.smoothDeltaTime*0.5,-horizontalLimit,horizontalLimit);
scrollDistanceY = Mathf.Clamp(scrollDistanceY+scrollDeltaY*Time.smoothDeltaTime*0.5,-verticalLimit,verticalLimit);
targetItem.transform.position.x = scrollDistanceX;
targetItem.transform.position.y = scrollDistanceY;
}
}
}
}
Answer by chirpytime · Dec 13, 2014 at 08:52 AM
A really old question; I'm curious if this can be solved by making the object's collider larger, or using a SphereCast instead of Raycast so it's less sensitive to mouse position changes?
Your answer
![](https://koobas.hobune.stream/wayback/20220612150036im_/https://answers.unity.com/themes/thub/images/avi.jpg)