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

Rotating player to target rotation.

Discussion in 'Scripting' started by cristo, Sep 24, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi,
    I'm trying to work out how to rotate the player towards the target transform's rotation, as well as the target's position.

    Any feedback would be great.


    Code (CSharp):
    1. void Update() {
    2.  
    3.         playerDistance = Vector3.Distance(player.position, target.transform.position);
    4.         playerRotation = Vector3.Quaternion.Euler.Rotation(player.rotation, target.transform.rotation);
    5.         if (playerDistance < 15f)
    6.         {
    7.            
    8.             player.transform.rotation = Vector3.RotateTowards (player.transform.rotation, target.transform.rotation, moveSpeed * Time.deltaTime);
    9.                 //Quaternion.Euler(0f, 0f, 0f);
    10.             //  player.transform.position = new Vector3(3.5f, 2f, 13.8f);
    11.             player.transform.position = Vector3.MoveTowards(player.transform.position, target.transform.position, moveSpeed * Time.deltaTime);
    12.  
    13.           }
    14. }
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
  3. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90