Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

HeadLookController Modified

Discussion in 'Scripting' started by DRRosen3, Oct 15, 2015.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I've been trying to put this asset to use in my project: Head Look Controller

    The controller as is uses a targeting system. You attach a script to an object and it makes the characters look at that object. What I want to achieve is having the characters look at a point in space based on where the mouse cursor is. I want to offset that point by (Character.transform.position.z + SkinnedMeshRenderer.bounds.extents.z). I've tried doing it the following way but I'm at a loss for what I'm doing wrong.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Head_Look_Target : MonoBehaviour
    5. {
    6.     public HeadLookController headLook;
    7.     public SkinnedMeshRenderer skinnedRenderer;
    8.  
    9.     private float offset = 1.0f;
    10.    
    11.     void LateUpdate()
    12.     {
    13.         offset = transform.position.z + skinnedRenderer.bounds.extents.z;
    14.  
    15.         Vector3 target = Input.mousePosition;
    16.         target.z = offset;
    17.  
    18.         headLook.target = target;
    19.     }
    20. }
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    BUMP?
     
  3. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    will get you started in the right direction I think. The mouse is in screen coordinates, not camera coordinates. The ray will point into the scene in the direction of the point you want to look at. I leave you to the rest of the math.