Search Unity

Mouse Click RayCast ground position to Quaternion.?

Discussion in 'Scripting' started by ryandewitt, Nov 20, 2007.

  1. ryandewitt

    ryandewitt

    Joined:
    Oct 6, 2007
    Posts:
    10
    Hey everyone. I'm just getting started with all of this. Could someone please direct me on my bad script that is supposed to rotate my character when the mouse clicks an area on the ground mesh. I have a yellow ray casting (debug) but I can not figure out the hit location conversion.

    right now, If you click the character he rotates in every direction.

    any help is appreciated.
    thx.

    http://pivotlabs.net/unity/pivot_rayCast_1.unityweb.html


    Code (csharp):
    1.     var target : GameObject;
    2.     //var target : Transform;
    3.     var hit : RaycastHit;
    4.     var damping = 6.0;
    5.     var aimVector = new Vector3(0.0, 0.0, 1.0);
    6.  
    7.     function Update ()
    8.     {
    9.             ray = camera.ScreenPointToRay (Input.mousePosition);
    10.             //just so we can see it's working
    11.             Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
    12.            
    13.            
    14.             if (!Input.GetMouseButton (0))
    15.             {
    16.                 Debug.Log("mouse_none");
    17.                 return;
    18.             }
    19.            
    20.             Debug.Log("mouse_down");         
    21.             if (!Physics.Raycast(ray,  hit, 100))
    22.             {
    23.                 Debug.Log("ray working");
    24.             }  
    25.            
    26.             rotateCharacter();
    27.     }
    28.  
    29.     function rotateCharacter() {
    30.                 //hey this is working
    31.                 Debug.Log("rotatecharacter function has been called");
    32.  
    33.                 //below works badly
    34.                 target.transform.rotation = Quaternion.FromToRotation (aimVector, hit.normal);
    35.                 //target.transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
    36.  
    37.     }
     

    Attached Files:

  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The main problem is that you're using hit.normal, which is the direction the surface is pointing in. You'd want to use hit.point instead, which is where you're clicking. How about:

    Code (csharp):
    1. var target : Transform;
    2. private var hit : RaycastHit;
    3. var useLayer : LayerMask;
    4.  
    5. function Update () {
    6.     if (Input.GetMouseButton(0)) {
    7.         var ray = camera.ScreenPointToRay (Input.mousePosition);
    8.         if (Physics.Raycast(ray, hit, 100, useLayer.value)) {
    9.             hit.point.y = target.position.y;
    10.             target.LookAt(hit.point);
    11.         }
    12.     }
    13. }
    Make a new layer, and put the ground mesh on that layer. Then, for useLayer in this script, select that layer. This way the raycast will only hit the ground and nothing else.

    --Eric
     
  3. ryandewitt

    ryandewitt

    Joined:
    Oct 6, 2007
    Posts:
    10