- Home /
Change output rotation of this Lookat script?
{
Quaternion _lookRotation =
Quaternion.LookRotation((to - transform.position).normalized);
transform.rotation =
Quaternion.Lerp(Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 90), _lookRotation, Time.deltaTime * turn_speed);
}
This will always turn from the Z side, though i'd like to have it turn from the X side? how could i change it?
full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate : MonoBehaviour {
public float turn_speed;
public float Find_radius;
public string TagToSearchFor;
[Header ("Front, Left, Back, Top, Bottom")]
[Tooltip("Z points to front")]
public string Direction;
private Collider[] all_found;
private Transform Target;
private void check() {
all_found = Physics.OverlapSphere(gameObject.transform.position, Find_radius);
if(all_found.Length != 0)
{
foreach(Collider col in all_found)
{
if(col.tag == TagToSearchFor)
{
Target = col.gameObject.transform;
break;
}
}
}
}
protected void rotateTowards(Vector3 to)
{
Quaternion _lookRotation =
Quaternion.LookRotation((to - transform.position).normalized);
transform.rotation =
Quaternion.Lerp(Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 90), _lookRotation, Time.deltaTime * turn_speed);
}
private void Update()
{
try
{
rotateTowards(Target.position);
}
catch
{
check();
}
}
}
Side note: It's standard c# practice to name your classes and functions with PascalCase
(Capital letter at start) rather than camelCase
(lower case letter at start).
Answer by ProtoTerminator · Dec 10, 2018 at 02:19 AM
Quaternion.LookRotation accepts a forward direction and an optional up direction. If you don't provide an up direction, it defaults to Vector3.up. So you can provide your own up direction (Vector3.right/Vector3.forward/etc).
[EDIT] After re-reading your question, it looks like you want to have the transform's X axis look at the target instead of the transform's Z axis. According to the documentation, LookRotation makes the transform look on its Z axis. So all you need to do is rotate your transform 90 degrees after you set the lookrotation. transform.rotation = Quaternion.LookRotation((transform.right - transform.position).normalized);
or, even easier transform.Rotate(0, 90, 0);
[EDIT2] Here's the code (from my comment):
protected void rotateTowards(Vector3 to)
{
Quaternion _lookRotation = Quaternion.LookRotation((to - transform.position).normalized);
transform.Rotate(0, -90, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, _lookRotation, Time.deltaTime * turn_speed);
transform.Rotate(0, +90, 0);
}
In any case, it's probably easier to just parent the object you're trying to rotate under an empty gameobject, and just rotate the empty gameobject.
If i use Quaternion.LookRotation((transform.right - transform.position).normalized);
It'll still look from the Z axis. While transform.Rotate(0, 90, 0);
has some sort of effect, it bugs it alot.
Would that be me, or is it bugged in general?
Oh I made a mistake with the lookrotation. I believe it should just be Quaternion.LookRotation(transform.right)
. What sort of effect does transform.Rotate have? That should simply instantly rotate it 90 degrees.
$$anonymous$$aybe it's because you should Rotate -90 degrees before doing the lerp rotation, then after the lerp Rotate +90 degrees. I'll update my answer for that code.
Answer by nicholasw1816 · Dec 10, 2018 at 02:29 PM
Try this then tell me what it does. You have to declare a up direction. Try one of these till it works. Vector3(1,0,0), or Vector3(0,1,0), or Vector3(0,0,1)
Quaternion.LookRotation((to - transform.position).normalized, Vector3(0,0,1));
Your answer
Follow this Question
Related Questions
Change output rotation of this Lookat script? 0 Answers
Flip over an object (smooth transition) 3 Answers
Look Rotation flipping object rotation after 180 degrees 1 Answer
Rotate/Orbit around an object to look at another object C# 1 Answer
Rotation via iTween.LookTo not working (or using it wrong) 1 Answer