Hey guys, I don’t know if this is as simple as it sounds or not…
I want to find an object’s rotation relative to a vector3 instead of world? Kinda like the localRotation…
Does that make sense? Thanks in advance guys.
Hey guys, I don’t know if this is as simple as it sounds or not…
I want to find an object’s rotation relative to a vector3 instead of world? Kinda like the localRotation…
Does that make sense? Thanks in advance guys.
Maybe this could help.
http://unity3d.com/support/documentation/ScriptReference/Vector3.Angle.html
Hmmm it might, though i’m not sure how would i implement it.
Let me be a bit more specific:
I have a vehicle (tank) driving around a curvy terrain, and the turret is to look at the mouse pointer. My solution for that is
lookAngle=Quaternion.LookRotation(mousePos.point-transform.position).eulerAngles.y;
transform.localEulerAngles=Vector3(0,0,lookAngle);
But I need to find the tank body’s rotation on the same plane as the turret.
Does that make more sense? Any ideas?
No. Are you trying to limit turret’s vertical rotation?
No no, the turret traverses 360 degrees.
Say if the tank points straight forward and the mouse pointer is to the right, the turret is rotated correctly, 90 degrees right.
But if i turn the tank 90 degrees right and the mouse pointer is in the same position, the turret still points 90 degrees right relative to the body.
So i need the body’s rotation and subtract it from lookAngle to get the true relative bearing on the turret.
BUT, I can’t just use
bodyAngle=transform.eulerAngles.y;
because that breaks when the tank goes up and down a hill, because the return is the y angle relative to the world, whereas i need the return to be relative to the plane of the same orientation as the turret.
Thanks Digital
Ok, first check if turret’s Z axis is pointing forward, make turret tank’s child object and now for turret rotation use Transform.LookAt. You don’t need tank’s rotation.
Yes, but the other thing here is that the turret is supposed to pivot only on one axis, so just good old Transform.LookAt doesn’t really work…
Make turret from 2 objects, turret and gun. Now when you rotate turret set vertical(X) rotation to 0 and for gun set horizontal(Y) rotation to 0, after Transform.LookAt.
U know, I’ve tried that and it didn’t work. It seems like LookAt works only if all 3 axis are looking, if i force localEulerAngles.x and .y to 0, the turret does pivot along the right axis but does not look at the mouse the slightest bit, usually makes a full rotation in just a slightest mouse movement
Resolved, but kinda janky… Here’s my solution, but if maybe someone can come up with something cleaner, i’d appreciate it.
public var frontHelper:Transform;
public var sideHelper:Transform;
private var mousePos:RaycastHit;
private var aimOffset:Vector3=Vector3(0,1,0);
function FixedUpdate ()
{
var parentTransform:Transform=transform.parent.transform;
var lookAngle:float;
var orientation:float;
var orientationVector:Vector3=sideHelper.position-parentTransform.position;
var smoothVector:float;
mousePos=RaycastHit();
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),mousePos);
orientation=Quaternion.LookRotation(frontHelper.position-parentTransform.position, orientationVector).eulerAngles.y;
lookAngle=Quaternion.LookRotation(mousePos.point+aimOffset-transform.position).eulerAngles.y-orientation;
smoothVector=Mathf.LerpAngle(transform.localEulerAngles.z,lookAngle, Time.deltaTime*5);
transform.localEulerAngles=Vector3(0,0,smoothVector);
}
Where frontHelper and sideHelper are empty gameObjects, frontHelper is aligned to body’s pivot and moved out to the front of it, sideHelper is aligned and moved out to the side of the body.
What that does is give me a LookRotation which is essentially the rotation relative to the local axis of the body.
What do u think guys, is there a better way to achieve this result? Thanks