- Home /
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.
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;
 }
Also you said "supposedly locked" Locks are for rigidbody physics interactions. They don't do anything when you translate and rotate transforms.
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
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);
         }
     }
$$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.
 Vector3 lookDirection = target.position - player.position; // this vector points from player to target
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);
 
Attempted this solution as well, however it didn't seem to work either.
I just tried this same solution and it worked perfectly. Can you show us your implementation of it?
Wait a $$anonymous$$ute. This solution is fundamentally equivalent to one I already gave. Definitely show us your full implementation of this solution.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                