- Home /
Getting the rotation of raycast target object and applying to the player (noob question)
Hello everyone, So I'm a beginner at programming and watched some tutorials and examples but couldn't find this one thing... I have a script that uses a raycast (to interact with objects) attached to the player. So now I have a certain object that I want to interact with and get that object's rotation information the forward or Y-rotation, and apply it to the player. so the forward of (for example) a cube is set the same for the player. for now I got this:
void Update () {
Debug.DrawRay(this.transform.position, this.transform.forward * interactionDistance, Color.magenta);
if(Physics.Raycast(this.transform.position, this.transform.forward, out interactWhat, interactionDistance))
{
if (Input.GetButton("Interact"))
{
if (interactWhat.collider.gameObject.name == "StairsTop")
{
gameObject.GetComponent<Transform>().localRotation ...
}
}
}
}
So I don't know how to go further on that line, or if I maybe approaching it wrong. And yea, that object object with collider is called (StairsTop)
Thanks for reading!
Answer by hacky97 · Dec 12, 2018 at 08:44 AM
the interactWhat is of type RaycastHit, which has a transform property of the object you hit.
transform.localRotation=interactWhat.transform.localRotation;
Lookup raycasthit here.
Thanks for the help! I was able to fix my problem just a little after I posted this. Your answer is maybe a shorter version of my solution, so thank you for showing it this way too! : )