Highlighting and Moving an Object

Hello World! :slight_smile:

I’m making a simple pvp 3D chess game and I have a few problems…

First of all, I would like to be able to click and drag a chess piece, so when clicked (while holding mouse button down) it automatically moves “up” just above other pieces and then I should be able to drag it around, and place it somewhere upon mouse button release.
All I have is this code, which moves it awkwardly:

private var screenPoint: Vector3;
    private var offset: Vector3;
    private var curScreenPoint : Vector3;
    private var curPosition : Vector3;
    
    function Start () {
    }
    
    function Update () {
    }
    
    function OnMouseDown () {
        screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        Screen.showCursor = false;
        }
    
    function OnMouseDrag() { 
        curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
        }
    
    function OnMouseUp(){
        Screen.showCursor = true;
        }

Secondly, I would like to highlight an object using (I guess) OnMouseEnter and OnMouseExit, but again my code doesn’t work! I have no errors, but it simply won’t change my predefined defaultMaterial to OnHoverMaterial…?

    var defaultMaterial : Material;
    var onHoverMaterial : Material;
    
    function OnMouseEnter(){
        defaultMaterial = renderer.material;
        renderer.material = onHoverMaterial;
    }
    
    function OnMouseExit(){
        renderer.material = defaultMaterial ;
    }

Thanks guys! :slight_smile:

I’m not sure what is going on with your highlight. The code looks fine and works for me. Put a Debug.Log() statement inside the OnMouseEnter() to see if it is even being called. If not, replace a chess piece with a the Unity cube and test it. If the cube also has issues, I’d look for a collider between the camera and the box.

As for moving the chess piece, the trick here is to translate Y movement into Z movement. Assuming your chess board is on the XZ plane, you can save the initial position and then use the Y delta from that position as a Z delta from the Z’s original position to move the piece back to front. The Y position would not change. As for picking the piece up, if you can live with an immediate jump, just translate your piece up right before line 14 by a fixed amount, and then translate it back down by the same amount in the OnMouseUp() function.

Ok, I got highlight material to work, my pieces move up on y axis when clicked and move along x and z when dragged, I disabled rotation when dragged and put a Debug.DrawRay to see where the piece will fall.

But, this last thing bugs me. Instead of DrawRay, it would be nicer if the area beneath the piece, where the shadow would be projected on the chess board, could ‘glow’ let’s say red.
For what I learned so far, I’d need to use RaycastHit, so the code would look something like this:
function Update(){

var rayDown = transform.TransformDirection(0, 0, -5);
var hit : RaycastHit;

if(Physics.Raycast(transform.position, rayDown, 5)){

      if(hit.collider.gameObject.name == "Board"){

             //Somehow make a glow highlight on board

      }

}
}