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

(solved) Rotating object according to position on screen?

Discussion in 'Scripting' started by JamesThornton, Mar 22, 2016.

  1. JamesThornton

    JamesThornton

    Joined:
    Jun 26, 2015
    Posts:
    52
    I would like to rotate an object between two values as it moves across the screen.

    All the way on the left would be at -30 rotation on the y. All the way on the right would be 30.

    Then smoothly blend between them as the object moves back and forth across the screen.


    I already have the following script, which debugs it's location. I just can't figure out how to connect rotation values to position.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerAim : MonoBehaviour {
    5.     public Transform target;
    6.  
    7.  
    8.     void Update() {
    9.         Vector3 screenPos = Camera.main.WorldToScreenPoint(target.position);
    10.         Debug.Log("target is " + screenPos.x + " pixels from the left");
    11.     }
    12. }
    Any help understanding is greatly appreciated!
     
  2. JamesThornton

    JamesThornton

    Joined:
    Jun 26, 2015
    Posts:
    52
    Solved using ViewPortPoint (instead of ScreenPoint), and a lerp w/ euler angles. I guess that makes more sense because viewport is 0-1