- Home /
making an object below this object follow rotation on y axis
So I've been trying to get a simple script done where I have a little animated mesh that is supposed to become visible when the player comes close enough to the ground, and follows the rotation of the player's ship on the Y axis. I've tried the following, but for some reason there is absolutely no rotation on the animated mesh at all. The rest is working fine, but for some reason I cannot seem to fix the rotation of this thing.
#pragma strict
var maxDistance: int = 30;
var dustFX: GameObject;
private var dustInstance: GameObject;
function Awake()
{
if(dustFX==null)
{
this.enabled = false;
}
dustInstance = Instantiate(dustFX,transform.position,transform.rotation);
dustInstance.SetActive(false);
}
function Update()
{
var hit: RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit))
{
if(hit.distance<maxDistance)
{
dustInstance.SetActive(true);
dustInstance.transform.position = hit.point;
var newRotation = Quaternion.LookRotation(dustInstance.transform.position - transform.forward*10);
newRotation.x = 0.0;
newRotation.z = 0.0;
dustInstance.transform.rotation = Quaternion.Slerp(dustInstance.transform.rotation, newRotation, Time.deltaTime * 8);
}
else if(hit.distance>maxDistance)
{
dustInstance.SetActive(false);
}
}
}
To clear things up a bit, this script is attached to the player ship, so transform refers to the player ship's transform. What I think I'm doing is making a lookRotation from the dustInstance to a point 10 units from the player ship's front. Then the X and Z values of that rotation are set to 0, to make it a Y only rotation. Then I apply the rotation with Quaternion.Slerp.
Anyone see what I'm doing wrong here?
EDIT: okay, so after some asking around I found out that we'd want the animated mesh to be under the ship, using the normal of the raycasthit to position it. however this makes the whole rotation thing even harder. Here's the current script:
#pragma strict
var maxDistance: int = 30;
var dustFX: GameObject;
function Awake()
{
if(dustFX==null)
{
this.enabled = false;
}
dustFX.SetActive(false);
}
function Update()
{
var hit: RaycastHit;
if (Physics.Raycast (transform.position, -transform.up, hit))
{
if(hit.distance<maxDistance)
{
dustFX.SetActive(true);
dustFX.transform.position = hit.point;
dustFX.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
dustFX.transform.localEulerAngles.y = transform.localEulerAngles.y;
}
else if(hit.distance>maxDistance)
{
dustFX.SetActive(false);
}
}
}
Changed a bunch of things, the dustFX object is now a child object of the player ship, and is just being moved to the hit normal when the ship is close to something. I've tried changing the y axis rotation of the dustFX object to the ship's y axis, but this didn't work. Strange rotations and such.
I have no idea how to get this to work now, but I'm gonna continue fiddling with it. Maybe I'll find a way, haha!
EDIT(again):
I believe the best way to do this would be to align only the z and x axis to the hit normal, however fromtorotation does not seem to work for that purpose.