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. Dismiss Notice

Resolved FIXED transform.rotation.y is always somehow a problem.

Discussion in 'Scripting' started by enesbagci2332, Jul 22, 2023.

  1. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    Code (CSharp):
    1.  
    2.         direction = (player.transform.position - transform.position).normalized;
    3.  
    4.  
    5.         float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
    6.  
    7.  
    8.         Quaternion rotateToTarget = Quaternion.AngleAxis(angle, Vector3.up);
    9.  
    10.  
    11.         transform.rotation = Quaternion.Slerp(transform.rotation, rotateToTarget, Time.deltaTime * turningSpeed);
    12.  
    The enemy always looks at my player! This should work, right? But it doesn't work for the y-axis.


    DEEP NOTE: Just to move my player forward I have to use Vector3.Right. Idk why but it is broken too.

    Can you guys help me?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.         direction = (player.transform.position - transform.position).normalized;
    2.         Quaternion rotateToTarget = Quaternion.LookRotation(direction.normalized);
    3.         transform.rotation = Quaternion.Slerp(transform.rotation, rotateToTarget, Time.deltaTime * turningSpeed);
     
    enesbagci2332 and Yoreki like this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    I wanted to mention LookRotation aswell. Further, you can also directly set the transform.forward direction, which will handle the rotation automatically. For instant rotations that's a good solution, but if all else fails you could also just slerp the forward vectors.

    What do you mean by that? In a 2D game that would be normal behavior. Assuming it's 3D, you probably messed up the orientation of either your model, or the child hierarchy of your player. It's hard to tell without a more precise problem description.
     
    enesbagci2332 likes this.
  4. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    Thanks but didn't change anything at all.
     
  5. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    I tried the LookRotation method but still it gives me the same results. It looks like there is somehow off.
     
  6. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    I recorded a video, showing the problem. I hope this will help you guys understand my problem.
     

    Attached Files:

  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I see you wrote this in your subject:

    But I don't see you touching that in code.

    You should NEVER touch the .x, .y, .z and .w components of a rotation. They are NOT angles.

    If meant instead that you are touching
    transform.rotation.eulerAngles.y
    , you're ALSO going to have a bad time, so don't do that. Here's why:

    All about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

    If this is a 2D game, then your LookRotation should probably use Vector3.forward as its "up" vector.
     
    enesbagci2332 likes this.
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    First of all, when showing others a video, it's best to upload it to youtube or a similar platform. When posting files on the forum, the people who are willing to look at them (at all) have to go through unnecessary steps, such as checking for viruses.

    As i originally stated however, your model is simply not correctly oriented. "Forward" is the blue Z-axis. Your model is oriented towards the red X-axis. The code should work as expected. I dont do modelling, but im pretty sure most tools such as blender use a different coordinate system than unity, so importing / exporting correctly you probably have to flip some axis. You could also add your model to a child of your actual player, which would allow you to correct the rotation locally.
     
    enesbagci2332 likes this.
  9. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82


    I DID IT, I FIXED IT. (With everyone's help of course.)

    Code:

    Code (CSharp):
    1.  
    2.  
    3.         Quaternion lookRotation = Quaternion.LookRotation(playerPropeller.transform.position - transform.position);
    4.         Vector3 goToThePos = (player.transform.position - transform.position).normalized;
    5.  
    6.  
    7.         transform.rotation = lookRotation;
    8.         transform.Rotate(lookRotation.x, lookRotation.y + 93, lookRotation.z, Space.World);
    9.  
    10.         rb.velocity = new Vector3(goToThePos.x * speed, goToThePos.y * speed, goToThePos.z * speed);
    11.  
    12.  
    Special credit to: Yoreki for code ideas and Kurt Dekker for the awesome scripting knowledge!
     
    Kurt-Dekker likes this.
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Are you sure this works? Your 'lookRotation' is a quaternion. Their x,y,z,w values are not angles. Treat them as magic numbers. They also only range from -1 to 1. You pass them to transform.Rotate, which expects angles. So this shouldnt do a whole lot. The manual offset by 93 is also a bit weird.

    The code posted by @zulo3d above should work. To counteract your 90° offset (from the swapped x and z axis) you should just put the model into a separate gameobject and make that a child of your actual player or enemy gameobject, and add the correct rotation to that child so the model is visually correctly oriented. Or import the model correctly, but i cant comment on how.
     
    enesbagci2332 likes this.
  11. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    It it works, it is not broken. Still though for further improving I will try your suggestions.
     
  12. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    Sure, I will do that next time I upload and again thanks for telling me about this, I personally exported that plane model from blender. I have never tought about it using diffrent axis.

    Thank you!
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Feels good, doesn't it? :)

    For future reference here is a super simple super-generic approach to figure out what is going on with just about any piece of code you encounter...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    enesbagci2332 likes this.
  14. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    I was doing using debug.log stuff and all but I didn't know anything about "Debug.DrawRay() or Debug.DrawLine()" and "You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc." again, thank you so much!

    Unity really does have a great community, thanks everyone.
     
    Yoreki and Kurt-Dekker like this.