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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

need logic help..

Discussion in 'Scripting' started by NewSystemGames, Mar 26, 2018.

  1. NewSystemGames

    NewSystemGames

    Joined:
    May 23, 2017
    Posts:
    303
    I have this little line which just looks at a player target

    Code (CSharp):
    1.             Quaternion lookOnlook = Quaternion.LookRotation(player.transform.position - transform.position);
    2.             transform.rotation = Quaternion.Slerp(transform.rotation, lookOnlook, lookSpeed);
    I been trying to have the rotation only apply on Y and Z and X stays unchanged, but i can't get it to work, anyone can help here?
     
  2. EpsilonQ

    EpsilonQ

    Joined:
    Mar 26, 2018
    Posts:
    5
    Check out: https://forum.unity.com/threads/freeze-rotation-does-not-let-the-character-spin-what-to-do.523616/

    Pretty much the same issue.

    Don't want to read?

    Code (CSharp):
    1.         Quaternion lookOnlook = Quaternion.LookRotation(player.transform.position - transform.position);
    2.         lookOnlook.x = transform.rotation.x;
    3.         transform.rotation = Quaternion.Slerp(transform.rotation, lookOnlook, lookSpeed);
    though this might actually give you a little more control because slerp may still modify the x-axis;

    Code (CSharp):
    1.         Quaternion lookOnlook = Quaternion.LookRotation(player.transform.position - transform.position);
    2.         Quaternion newRotation = Quaternion.Slerp(transform.rotation, lookOnlook, lookSpeed);
    3.         newRotation.x = transform.rotation.x;
    4.         transform.rotation = newRotation;
    It kind of looks l like you have a humanoid character, so you may consider just having it set to a constant value as opposed to getting the previous rotation. So it's always at the right angle.

    Code (CSharp):
    1.         Quaternion lookOnlook = Quaternion.LookRotation(player.transform.position - transform.position);
    2.         Quaternion newRotation = Quaternion.Slerp(transform.rotation, lookOnlook, lookSpeed);
    3.         newRotation.x = playerXAxis
    4.         transform.rotation = newRotation;
     
    Last edited: Mar 26, 2018
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can try something like this:
    Code (csharp):
    1. Vector3 lookDir = player.transform.position - transform.position;
    2. lookDir.y = 0;
    3. Quaternion rot = Quaternion.LookRotation(lookDir);
    4. // etc..
    Though, I'd recommend that you use RotateTowards instead of Slerp; if you wish to use Slerp you should store the start and end rotation and use it ... more properly.
     
  4. EpsilonQ

    EpsilonQ

    Joined:
    Mar 26, 2018
    Posts:
    5
    His 'lookOnLook' is the end rotation.

    Also abandoning slerp for a direct transform modification tends to sacrifice the smoothness which is kind of the goal of slerping I would assume? Maybe give him the benefit of the doubt. :p
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It's a misuse of Slerp that everyone has done for a long time but isn't necessary anymore with RotateTowards. You can look in the manual. Slerp was made for a fixed start point and end point. It works like this, but it tapers off as it gets closer and closer to the destination, so it's actually not an even type of rotation. The only reason the thing actually stops rotating is because of roundoff error. I've done it myself and it works all right because computers are so fast your eye can't really see it. RotateTowards is better if you want to move an even amount each time rather than a shrinking percentage.