Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Otaku_Riisu · Jul 09, 2017 at 10:27 PM · rotationcharacter movementtargeting

Character rotating on supposedly locked axis

Right now I am trying to get my character to face a targeted enemy. Everything works great except for the fact my character will face the targeted enemy on every axis, not just the Y axis.

Here is an example of the bug.

I dont want the character to rotate to face the enemy downward, because of the other glitch i showed in the video where the player can walk backwards into the air. Here is the code I'm using to have the player look at the enemy.

 public void lookAtTarget(){
     if (target != null) {
         Vector3 lookDirection = new Vector3 (Player.position.x, target.position.y, Player.position.z);
         rig.transform.rotation = Quaternion.Lerp (rig.transform.rotation, Quaternion.LookRotation (lookDirection.normalized), Time.deltaTime * 12f);
         }
     }

Ive also tried variations of the lookDirection vector, including

 Vector3 lookDirection = new Vector3 (0.0F, target.position.y, 0.0F);

and

 Vector3 lookDirection = new Vector3 (target.position.x, 0.0F, target.position.z);

if anyone has any ideas on how to fix this, help would be very much appreciated. I cant seem to get it to work at the moment, no matter what else I try.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jwizard93 · Jul 09, 2017 at 11:16 PM

The Y direction is the "Up and Down" direction. You are only rotating the player to face the object as it goes up and down.

I'm not sure why you are getting X and Z targeting but maybe if I saw more code it would become clear.

Anyway if the target's y value goes below the player's y value just stop following it in the Y direction.

 Vector3 lookDirection = target.position - player.position; // this vector points from player to target
 
 if ( lookDirection.y < 0)
 {
     lookDirection.y = 0;
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jwizard93 · Jul 09, 2017 at 11:18 PM 0
Share

Also you said "supposedly locked" Locks are for rigidbody physics interactions. They don't do anything when you translate and rotate transforms.

avatar image Otaku_Riisu Jwizard93 · Jul 10, 2017 at 12:02 AM 0
Share

Sorry, I didnt mean rigidbody locked. I meant that they were locked by code (or, at least I thought they were). I should have specified

avatar image Otaku_Riisu · Jul 10, 2017 at 12:01 AM 0
Share

Here is everything that the character uses to move/look around. I hope this helps. I tried the code you suggested, setting lookDirection.y = 0, but the same thing still happens. I honestly have no idea why this isn't working ^^;

     void Update () {
         movement ();
         movementAction ();
         targetObject ();
         combat ();
         if (anim.GetFloat ("ComboProgress") > .05) {
             anim.SetFloat ("ComboProgress", anim.GetFloat ("ComboProgress") - 1 * Time.deltaTime);
         }
         if (anim.GetFloat ("ComboProgress") > 1) {
             anim.SetFloat ("ComboProgress", 1); 
         }
     }
 
     public void movement(){
     //$$anonymous$$ulti-Use Variables
         float horizontal = Input.GetAxis ("Horizontal"); 
         float vertical = Input.GetAxis ("Vertical");
         anim.SetFloat ("Horizontal", horizontal);
         anim.SetFloat ("Vertical", vertical);
         Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);
         
     //Running
         float joystick$$anonymous$$agnitude = $$anonymous$$athf.Clamp01 (new Vector2 (horizontal, vertical).magnitude);
         anim.SetFloat ("Speed", joystick$$anonymous$$agnitude);
 
     //Turning
         Vector3 nothing = new Vector3 (0,0,0);
         Vector3 cameraDirection = cam.transform.forward;
         cameraDirection.y = 0.0f;
 
         Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
         Vector3 moveDirection = referentialShift * stickDirection;
         if (target != null) {
             lookAtTarget ();
         } else {
             if (anim.GetCurrentAnimatorStateInfo (0).IsTag ("Evasion")) return;
             if (moveDirection == nothing) return;
             rig.transform.rotation = Quaternion.Lerp (rig.transform.rotation, Quaternion.LookRotation (moveDirection.normalized), Time.deltaTime * 12f);
         }
     }
 
     public void movementAction(){
         if (Input.GetButton ("Sprint")) {
             anim.SetFloat ("Speed", 1.2f);
         }
         if (Input.GetButtonDown ("Sprint") && target == null) {
             anim.Play ("sprinting_forward_roll");
         } else {
             //anim.Play ("dodge");
         }
     }
 
     public void lookAtTarget(){
         if (target != null) {
             Vector3 lookDirection = new Vector3 (Player.position.x, target.position.y, Player.position.z);
             if ( lookDirection.y != 0)
             {
                 lookDirection.y = 0;
             }
             rig.transform.rotation = Quaternion.Lerp (rig.transform.rotation, Quaternion.LookRotation (lookDirection.normalized), Time.deltaTime * 12f);
         }
     }


avatar image Jwizard93 Otaku_Riisu · Jul 10, 2017 at 12:08 AM 0
Share

$$anonymous$$e neither but start with this.

 Vector3 lookDirection = new Vector3 (Player.position.x, target.position.y, Player.position.z)

This does not make a vector pointing towards the target. I gave you the proper line.

avatar image Jwizard93 Jwizard93 · Jul 10, 2017 at 12:09 AM 0
Share
 Vector3 lookDirection = target.position - player.position; // this vector points from player to target
Show more comments
avatar image
1

Answer by Cornelis-de-Jager · Jul 10, 2017 at 12:45 AM

Try this:

    Vector3 lookPos = target.position - transform.position;
     lookPos.y = 0;
     
     Quaternion rotation = Quaternion.LookRotation(lookPos);
     
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Otaku_Riisu · Jul 10, 2017 at 12:49 AM 0
Share

Attempted this solution as well, however it didn't seem to work either.

avatar image Jwizard93 Otaku_Riisu · Jul 10, 2017 at 01:09 AM 0
Share

I just tried this same solution and it worked perfectly. Can you show us your implementation of it?

avatar image Jwizard93 Jwizard93 · Jul 10, 2017 at 01:13 AM 0
Share

Wait a $$anonymous$$ute. This solution is fundamentally equivalent to one I already gave. Definitely show us your full implementation of this solution.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger diagonal rotation - Problem 2 Answers

Rotating player with Quaternion and RotateTowards 1 Answer

Character facing the position of mouse cursor (2d platformer),Sprite changing based on mouse position 1 Answer

Cannot get my character to rotate on Z axis. --Top Down Character View 0 Answers

Stop rotation if a key is pressed 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges