Make part rotate away from Player

I’ve always wondered what type of CFrame magic or Rotation Math is required to achieve this sort of effect. I’ve been looking through many pages of the Devforum but I can’t exactly get an answer for this.

I basically want to figure out how to possibly make a part rotate AWAY from the angle the player hits it. Here’s an example shown below of what I’m trying to achieve

Video Link

Other Video Uploaded To Youtube:

1 Like

really interesting by any chance have you found out?

1 Like

Thats pretty easy, just give the part some offset whenever the player is close.

I’ve never done this before, but part of what you would need to do involves producing a special cframe to perform this rotation. And it would be based on the players position and the other object’s (plant’s) position. Assuming the position is also the point at which it rotates (the bottom of the plant).

First off, you might need to derive a vector that points from the player to the plant, or vice versa (plant to player)

local directionToPlant = (playerPos - plantPos).Unit

Then, you can produce a new CFrame that points in this direction, with the assumption that “up is up”.

local playerToPlantCFrame = CFrame.lookAt(Vector3.zero,directionToPlant,Vector3.yAxis)

This will produce a CFrame that points from the player to the plant, and it’s “up vector” should be pointing straight up (just like the player). Crucially, it will also make the “right vector” point in the direction that would be the axis of rotation that you would want for the plant.
In other words, the axis of rotation the plant should rotate on to either rotate “away” or “towards” the player. It’s the vector that points perpendicular to the line between you and the plant, but not pointing up.

Honestly, you probably could do this without this particular CFrame :stuck_out_tongue:
Because that is what the cross product is for.

But depending on how you do this, you could use the CFrame to perform the rotation, but at this point you should have the info you need to perform the rotation you want.

you would just say somthing like:

local plantStartCframe = plant.CFrame -- we don't want to loose this

plant.CFrame = plantStartCframe * CFrame.fromAxisAngle(playerToPlantCFrame.RightVector,angleToRotate)

This should then perform the rotation on the plant. Some things may need to be swapped around if the effect is backwards. But this is the general layout of what needs to be done, from a mathematical perspective.

In fact, there might be a better way to do all this, because I am technically making 2 different cframes here.
Like I said, this is what the Cross Product is for.

You take 2 vectors, your “forward vector” and your “up vector” (which we assume are perpendicular to each other) and if you perform a Cross Product on those 2 vectors, you get a new vector which is perpendicular to both of those. Which in your case is the axis of rotation to rotate the plant.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.