1
votes

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. Managed 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.

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;
  }
}
1
Is the camera attached to anything? Check that the raycast isn't hitting anything that is close to the camera.Danny Herbert
Yes the camera is attached to the player controller, not sure this was the issueRyan Achten

1 Answers

1
votes

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")