The Code provided, allows the object “from”, to rotate and look at the object “target”. However, I want to change it so that the object “from” rotates to look at “target” with frozen rotation on Y axis which means that it wont be able to directly look at it due to this restriction… This is actualy what i want, on the same note, I did try to add a ridigdbody and use the option “freez rotation on Y axis”, but that did not work, and i think that is because the script overwrites it…
I am a tottal noob at this
Please help me fix my code
freezing rotation via Constraints does only work, if you rotate your rigid body by adding forces (in this case - rotating - AddTorque).
If you want to do This (rigidbody, forces, physics etc):
Brian Stones post - look for the PID controller, he attached a sample project
if you need this done via scripting, this could help you?
google “lookat only one axis unity”
All what I have written above is useful for limiting everything to one axis. (whoops)
If you want to lock just one axis, start with the methods above.
I would create one object (BlaBla_RotX) and another as it’s child (BlaBla_RotZ).
This way you have more control, and you should be able to apply at least the method by scripting (the 2nd one).
I am a bit in a hurry, but I have a few tips for you:
you should read about the differences of global-stuff (everything: position, eulerAngles, quaternions…) and local!
is your Mid-Turret a Child of something that is already turned by (another / the same) script? Yes? Then changes are high you want to add something to this rotation: Please check if changing transform.rotation to transform.localRotation already does the trick for you here.
Apart from this, when you are just rotating every object around one of it’s axis you could work with (local)eulerAngles - stuff. Quaternions are useful, and prevent gimbal lock (please google unity gimbal lock), but it’s hard to understand their behavior.
Always remember: you can SET (local)EulerAngles(x,y,z), but prevent reading from these values.
Example: something rotated on it’s y-axis around 90.0 degrees? Reading transform.eulerAngles.y could give you 90.0. Or -270.0 or 450.0 (360+90) …etc
thx again, I just got home and I will try the links you provided. I did however try localRotation just now, and it makes the turretHead spin infinately like a helicopter fan.
using UnityEngine;
using System.Collections;
public class TRRT : MonoBehaviour
{
public Transform turretBase;
public Transform turretHead;
public Transform target;
public float speed = 90f;
void Update ()
{
Vector3 targetDirection = Vector3.Normalize(turretHead.position - target.position); // Direction to target.
float baseAngle = Mathf.Atan2(targetDirection.x, targetDirection.z) * Mathf.Rad2Deg; // Angle around Y axis the base should rotate.
float lengthXZ = Mathf.Sqrt(targetDirection.x * targetDirection.x + targetDirection.z * targetDirection.z); // Hypotenusae of the x z triangle
float headAngle = Mathf.Atan2(lengthXZ, targetDirection.y) * Mathf.Rad2Deg; // Angle around X axos the head should rotate.
Quaternion baseRotation = Quaternion.AngleAxis(baseAngle, Vector3.up); // The desired localRotation of the base.
Quaternion headRotation = Quaternion.AngleAxis(headAngle, Vector3.right); // The desired localRotation of the head.
turretBase.localRotation = Quaternion.RotateTowards(turretBase.localRotation, baseRotation, speed * Time.deltaTime);
turretHead.localRotation = Quaternion.RotateTowards(turretHead.localRotation, headRotation, speed * Time.deltaTime);
}
}
Well thx for the reply, it looks very nice but it’s beyond my understanding. It seems like it’s almost there apart from it doesn’t look at the target, though both turret head and base do rotate in the desired manner.
Questions that i have are:
Do I place this script on turret head or base/mid-section? Or both?
Atm I have placed it on the turret head and the result is:
I placed it only on the turret head, no other scripts are active on any other object. The target is that red Cube, your solutions feels very close btw. I will be out so I won’t be able to work on this for 4 hours, though I would still be able to read your comments.
Thanks again and any further help would be very much appreciated.
It looks as if it’s set up just like how I tested it.
If you change pivot mode to Pivot instead of Center and keep Rotation mode as Local, does the mid section (base in my code) aim to point the blue axis towards the target?
Seems, I didn’t have my math quite in check.
try this:
public class TRRT : MonoBehaviour
{
public Transform turretBase;
public Transform turretHead;
public Transform target;
public float speed = 90f;
void Update ()
{
Vector3 targetDirection = Vector3.Normalize(target.position - turretHead.position); // Direction to target.
float baseAngle = Mathf.Atan2(targetDirection.x, targetDirection.z) * Mathf.Rad2Deg; // Angle around Y axis the base should rotate.
float lengthXZ = Mathf.Sqrt(targetDirection.x * targetDirection.x + targetDirection.z * targetDirection.z); // Hypotenusae of the x z triangle
float headAngle = (Mathf.Atan2(lengthXZ, targetDirection.y) * Mathf.Rad2Deg) - 90f; // Angle around X axis the head should rotate.
Quaternion baseRotation = Quaternion.AngleAxis(baseAngle, Vector3.up); // The desired localRotation of the base.
Quaternion headRotation = Quaternion.AngleAxis(headAngle, Vector3.right); // The desired localRotation of the head.
turretBase.localRotation = Quaternion.RotateTowards(turretBase.localRotation, baseRotation, speed * Time.deltaTime);
turretHead.localRotation = Quaternion.RotateTowards(turretHead.localRotation, headRotation, speed * Time.deltaTime);
}
}
omg ur so fucking good! it worked, excuse my language.
Thank you so much, is there any where u can refer me to, so that I could understand how u made the script? after reading it 1000x over I understand most of it but I find this line the most difficult to understand: