Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reticle on quad doesnt rotate and face player

Discussion in 'Scripting' started by foxvalleysoccer, May 5, 2017.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Im trying to get my reticle cross hairs to rotate and face players gaze at all times.
    Any idea how to fix this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Reticle_Josh : MonoBehaviour
    6. {
    7.     public Camera cameraFacing;
    8.     private Vector3 originalScale;
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         originalScale = transform.localScale;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         RaycastHit hit;
    19.         float distance;
    20.         if (Physics.Raycast(new Ray(cameraFacing.transform.position, cameraFacing.transform.rotation * Vector3.forward), out hit))
    21.         {
    22.             distance = hit.distance;
    23.          //   Debug.Log(hit.collider.name);
    24.         }
    25.         else
    26.         {
    27.             distance = cameraFacing.farClipPlane * 0.95f;
    28.         }
    29.  
    30.         transform.position = cameraFacing.transform.position + cameraFacing.transform.rotation * Vector3.forward * distance;
    31.         transform.LookAt(cameraFacing.transform.position);
    32.         transform.Rotate(0.0f, 180.0f, 0.0f);
    33.         transform.localScale = originalScale * distance;
    34.     }
    35. }
    36.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Why not just make it a child of the camera? Then you can set transform.localPosition to (0,0,distance), and you never have to futz with its rotation at all.