#Rotating decal in the opposite direction of the player

1 messages · Page 1 of 1 (latest)

neat yoke
#

Hi everyone. I am awful with Quaternions! Basically, I am spawning a blood splatter decal on the floor which is supposed to face away from the player. I am using Quaternion.FromToRotation to get it on the ground, now I just need to rotate it towards the player. Any ideas?

Here's a picture demonstration. The red box outline represents the enemy, the black line representing the bullet, and the decal which is pointing away from the player

        if(Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 10)) {

            GameObject decal = GameManager.Instance.SplatterPool.SpawnDecal(new Vector3(hit.point.x, hit.point.y + 0.01f, hit.point.z), Quaternion.identity);
            //decal.transform.rotation = Quaternion.LookRotation(from.transform.position);
            decal.transform.rotation = Quaternion.FromToRotation(-Vector3.forward, hit.normal);
        }```
maiden relic
# neat yoke Hi everyone. I am awful with Quaternions! Basically, I am spawning a blood splat...

you can combine quaternion rotations with multiplications
but I'm not sure what you're trying to achieve here, because your second rotation would kinda invalidate the first
also, you can pass a second parameter to Quaternion.LookRotation, which is the up vector that should be used
you're also not using the parameters of it correctly
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
so for example, if you want it to look to the player, you determine the direction first. a direction is calculated by substracting the Start point from the End point.
start point here is the decal pos, end point is the player position
Vector3 decalToPlayer = (player.transform.position - decal.transform.position).normalized;
then use that as parameter
decal.transform.rotation = Quaternion.LookRotation(decalToPlayer);
if you want to invert the look direction you can simply negate it
decal.transform.rotation = Quaternion.LookRotation(-decalToPlayer);

(There is also transform.LookAt which you can use as well)

#

if you pass an upwards vector to LookRotation as second argument (which could for example be some vector along the ground), I think that helps with the rotation around the forward axis of the LookRotation if necessry

#

notice that the parameters are different, transform takes the world position of the target, quaternion LookRotation takes a direction

#

if you want to keep the decal ground aligned, you might want to use the hit.normal as forward direction input for Quaternion.LookRotation and set the up direction depending on the the direction from player to the decal.

#

it can also work to have a transform hierarchy, the decal prefab is an empty gameobject and the actual decal is below it
so you can do the alignment to the ground like you already have above, with the main empty object
and with the child decal, you can align it towards the player (the desired outcome is that only the local y rotation changes for the child), for example by calculating the direction from player to decal and projecting that direction along the ground

Vector3 playerToDecal = -decalToPlayer; // logic from above
Vector3 groundAlignedDir = Vector3.ProjectOnPlane(playerToDecal, hit.normal); // Project along ground plane
child.rotation = Quaternion.LookRotation(groundAlignedDir, child.transform.up); // passing transform.up to hopefully keep the up rotation like it already is
neat yoke
# maiden relic it can also work to have a transform hierarchy, the decal prefab is an empty gam...

I'm going with the transform hierarchy approach, and I'm running into a problem.

Vector3 decalToPlayer = (from.transform.position - decal.transform.position).normalized;

Vector3 playerToDecal = -decalToPlayer; // logic from above
Vector3 groundAlignedDir = Vector3.ProjectOnPlane(playerToDecal, hit.normal); // Project along ground plane
decal.transform.rotation = Quaternion.FromToRotation(-Vector3.up, hit.normal);
decal.transform.GetChild(0).rotation = Quaternion.LookRotation(groundAlignedDir, decal.transform.GetChild(0).transform.up);```

The child rotates towards the player, however only a sliver of the decal is shown because it isnt oriented properly
maiden relic