- Home /
Moving object with raycast issues
Hi Unity Community,
I have written a script where a gameobject is intended to move to a raycast.point thrown from the player camera. For the most part this works fine, however there are times (approximately when camera is 45 degrees up from the object) when the object rapidly moves towards the camera (i.e. raycast source).
I have tried a number of approaches attempting to resolve this, however I can’t seem to dig out the root of this issue. If anyone can provide any pointers as to where I am going wrong I would be incredibly grateful.
NB: coding in uJS
Many thanks in advance, Ryan
function FixedUpdate()
{
if (modObj != null && !guiMode){
//Panel Control
if (!selectObjPanel.activeSelf && !modifySelectObjPanel.activeSelf) //if the selectpanel not open and modSelect not already activated
{
activateModSelectObjPanel(true); //activate it
}
else if (selectObjPanel.activeSelf)
{
activateModSelectObjPanel(false);
}
//Move
if ( Input.GetKey(KeyCode.E))
{
if (Input.GetKeyDown(KeyCode.E))
{
// modObj.GetComponent(BoxCollider).enabled = false;
modObj.GetComponent(Rigidbody).isKinematic = true;
modObj.GetComponent(Rigidbody).useGravity = false;
//
initPos = modObj.transform.position;
var initRotation = modObj.transform.rotation;
}
moveObject(modObj, initPos, initRotation);
}
else{
// modObj.GetComponent(BoxCollider).enabled = true;
modObj.GetComponent(Rigidbody).isKinematic = false;
modObj.GetComponent(Rigidbody).useGravity = true;
}
}
}
function moveObject(modObj : GameObject, initPos : Vector3, initRotation : Quaternion)
{
//Debug.Log("Moving Object");
var hit : RaycastHit;
var foundHit : boolean = false;
foundHit = Physics.Raycast(transform.position, transform.forward, hit);
//Debug.DrawRay(transform.position, transform.forward, Color.blue);
if(foundHit && hit.transform.tag != "Player")
{
//Debug.Log("Move to Hit Point: " + hit.point);
modifyObjGUIscript.activateMoveDisplay(initPos, hit.point);
var meshHalfHeight = modObj.GetComponent.<MeshRenderer>().bounds.size.y /2; //helps account for large and small objects
// Debug.Log("CurObj Mesh Min: " + meshHalfHeight);
// modObj.transform.position = hit.point; //***method 01***
// modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02***
// modObj.transform.position = Vector3.SmoothDamp(initPos, hit.point, velocity, smoothTime); //***method 02***
var rb = modObj.GetComponent.<Rigidbody>();
rb.MovePosition(hit.point); //***method 03***
modObj.transform.position.y = modObj.transform.position.y + meshHalfHeight + hoverHeight;
modObj.transform.rotation = initRotation;
}
}
$$anonymous$$anaged to prevent this from occurring by deactivating the collider attached to the object being moved. However I need the collider for various reasons so this approach is not appropriate. Any alternative suggestions?
Answer by RyanAchtenSoma · May 12, 2016 at 03:04 AM
Turns out the issue was being caused by the raycast hitting the object being moved. Resolved this by only allowing hits from the terrain to be used as points to move to.
if(foundHit && hit.transform.tag == "Terrain")
Answer by aimmmmmmmmm · May 12, 2016 at 06:21 AM
The raycast hitting the object you are moving, causing the raycast's point to move towards the camera. Then the object moves, causing the point to move towards the camera again. Your best bet to solving this issue is to assign a layer to the object you are creating, and then create a layermask and use it during your raycasting to ignore the object you are moving.
Your answer
Follow this Question
Related Questions
Move player with mouse help 0 Answers
interesting raycast question 2 Answers
Collision point on surface? 1 Answer
RayCast relative to camera stays in 1 place. 2 Answers
Find a point at a distance from point on a line passing through two points 1 Answer