Affecting an object's position through another object's rotation
I have a floating gameobject (brickRot) that looks at what object is beneath it through a raycast, and then changes that object's y-position (floorY) based on brickRot's rotation. However, I'm having trouble converting rotation into a useful metric for changing Y-position. I want the current rotation of brickRot to be equal to whatever the y-position of floorY is, and then only change relative to the rotation of brickRot (if that makes any sense).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorRotation : MonoBehaviour {
private float brickRot;
private float floorY;
void Update () {
brickRot = transform.rotation.y;
RaycastHit hit;
Ray floorRay = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(floorRay, out hit)) {
if(hit.collider.tag == "floor") {
floorY = hit.transform.position.y;
floorY = floorY + brickRot;
Debug.Log(floorY);
}
}
}
}
Think of brickRot as a plate you put down on the table, and when you rotate the plate, you change the table's y-position (if that analogy makes it any less confusing). The reason why I'm using a raycast is because there are multiple "tables" that the brick can find, and it needs to differentiate between them. If anyone has an idea of how to make this any easier, I would be forever thankful!
Your answer
![](https://koobas.hobune.stream/wayback/20220612122229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
localPosition not where expected 1 Answer
Raycast is offsetting object 1 Answer
Unity zPosition hierarchy is making me insane. 0 Answers
Getting percentage value (of x position) between 2 gameobjects within a limit 1 Answer
How to calculate future position of animated object based on root motion animation? 0 Answers