#how to make an rotate a certain way off spawn

1 messages · Page 1 of 1 (latest)

ebon shadow
#

either use this to pass directly with the Quaternion rotation when you call Instantiate, or use Instantiate first and then set the trasnsform of the arm
you can either
set it so the transform axis of the arm is aligned with your look direction with
transform.up = or transform.forward = or transform.right = , depending on which axis you need to have aligned. https://docs.unity3d.com/ScriptReference/Transform-forward.html
the problem is this overwrites the rotation while you only set a single axis direction

if you want to define both the forward and the up axis, you can try to use either of those
https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
the first forward argument should take precedence I think, if the up direction is not ideally aligned
say you have a desired up direction that should take precedence though, over the forward, you can make sure the forward direction is 90 degree aligned to the up:

Vector3 upAlignedForward = Vector3.ProjectOnPlane(desiredForwardDirection, desiredUpDirection);
Quaternion rotation = Quaternion.LookRotation(upAlignedForward, desiredUpDirection);
arm.transform.rotation = rotation;

if you want to ignore certain directions the player is facing, you can try to project a direction onto the ground, for example, if you are only interested in the y rotation

Vector3 globalYAxisRotationDirection = Vector3.ProjectOnPlane(defaultDirection, Vector3.up);
ebon shadow
#

it would also help, if others read this, how your code looks like and how your visual component setup in the editor looks like (like what axis of the player or target object is the one you want to "align" the arm towards etc, where is the arm instantiated)

#

if you want to align it where the camera looks (that would be z direction of the camera)
if it should be aligned with the player body rotation though you need to figure out what axis that forward is you want to shoot for
ah I see

#

when you call instantiate you don't pass a parent transform right? the shot arm doesn't have a parent in the hierarchy right?

#

make sure this is set to local

then during playmode, pause the game after you shot an arm, and click on the arm,
check what axis is what direction
or just inspect the arm prefab instead

#

this button is to whether to translate an object in global or object local space

#

(and you don't need to afford anything, there is sooo much free content out there for learning unity etc)

#

can you click one object above, punching fist variant c?

#

because this position/pivot looks really weird of the child

#

with pivot i basically mean the transform origin

#

I mean it's strange that the gizmos with the arrows you see here is not directly on the arm
(but I guess that's because it might be something like this being the feet of a character that is basically invisible?)

#

can you click on PunchingFist_VariantC in the hierarchy on the left and make a screenshot how that looks in the scene view?

#

Ah, then it would be easier to manage if you reset the transform origin in blender too, to start at the arm

Usually I think the simple approach would be to take the camera directions for how to align the arm
you probably have a reference to the camera? if not get one
then you can try something like
spawnedArm.transform.forward = -camera.transform.forward;
it seems like you want the blue axis, but the inverse of it, blue is the z axis = forward axis. But the z axis goes from the hand to the sleeve, I assume you want it the other way around, so we invert the camera forward direction by putting a minus in front of it

#

If you also want to dictate the up direction to show in global up direction for example, you can do this
spawnedArm.transform.rotation = Quaternion.LookRotation(-camera.transform.forward, Vector3.up);

#

also I see, you don't need to do anything in blender, if the prefab is positioned so that the directions seem correct, which they seem to be, that's fine

#

????

#

where do you spawn the arm

#

okay either, I guess the spawnpoint could be used instead of the camera I wrote above
or you create a transform reference, and assign the camera to it

then, instead of spawnedArm, you call it cB, that seems to be the only difference though

hmmhh, it sounds like you should take some basic courses and tutorials before attempting a game on your own
of course you can still work on your game on the side, but it will be a pain to try to navigate it yourself. Including learning c#

#old_other message

#

cB.transform.forward = -spawnPoint.transform.forward;

#

something like that

#

I'd put it in line 25

#

or immediately after you Instantiate the arm

#

you add that to what you already have

#

imagine this, you leave what you have the same, the difference is that you run code that should rotate the arm correctly

#

maybe the axis isn't quite right,
maybe you need to set a different axis
cb.transform.right = -spawnPoint.transform.forward
or so

#

or you make it with the suggested offset from above
public Vector3 armRotationOffset; // on class level so you can assign it in the inspector
// and after setting the forward of cb, you can do the additional rotation
cB.transform.Rotate(armRotationOffset);

#

well it's cB

#

class level (watch some tutorials)

#

for c#

#

where you have defined your other public variables

#

nice