# Rotate Object relative to a other(I dont get it after 5 days google searching! pls)

Hi im realy, realy new so i try to master some stuff and it takes very long for me.
I made a Raycast check to detect the walls and it works fine.

Like this:

Wall run starts:
Player (Is moving 45° to the wall)

…\

…\

…\

Wall to wall run and it works fine

.

.

My Problem is:

How can i Rotate my player from this \ to this – ?

Like 45° away from the wall so the Player is in this direction:

.
.>

Player ---->

Wall

.
.

``````        RaycastHit doWallRunCheck(){
Ray rayRight = new Ray(transform.position, transform.TransformDirection(Vector3.right));
Ray rayLeft = new Ray(transform.position, transform.TransformDirection(Vector3.left));

RaycastHit wallImpactRight;
RaycastHit wallImpactLeft;

bool rightImpact = Physics.Raycast(rayRight.origin, rayRight.direction, out wallImpactRight, 0.45f);
bool leftImpact = Physics.Raycast(rayLeft.origin, rayLeft.direction, out wallImpactLeft, 0.45f);

if (Input.GetKey("w") && Input.GetAxis ("Horizontal") == 0f && rightImpact && Vector3.Angle
(transform.TransformDirection (Vector3.forward), wallImpactRight.normal) > 105f) {
//Debug.DrawRay (rayRight.origin, rayRight.direction * 10, Color.red);
rightWallRun = true;
startWallRun = 1;
return wallImpactRight;
}

else if (Input.GetKey("w") && Input.GetAxis ("Horizontal") == 0f && leftImpact && Vector3.Angle
(transform.TransformDirection(Vector3.forward), wallImpactLeft.normal) > 105f) {
wallImpactLeft.normal *= -1;

leftWallRun = true;
startWallRun = 1;
return wallImpactLeft;
}

else {
rightWallRun = false;
leftWallRun = false;
return new RaycastHit();
}
}

//Key combo conditions
if (leftWallRun) {
if (startWallRun >= 1 && canWallRun) {
if (Input.GetKey ("w") && Input.GetAxis ("Horizontal") == 0f) {
startWallRun = 2;
if (Input.GetButton ("Jump")) {
startWallRun = 3;
if (startWallRun >= 3) {
startWallRunMoveLeft++;
startWallRun = 0;
}
}
}
}
}
//left wall run starts to move
if (startWallRunMoveLeft >= 1){
//45° left will come there...
}
``````

PS: If some need a raycast to detect walls to finaly wall run, this one works fine for me.

Ok FINALY°! i got it
After 6 days testing and frustration about my programmer skills, but i got it somehow.
Maybe you need it, it works perfect for me.

there is my Script:

``````        RaycastHit doWallRunCheck(){
Ray rayRight = new Ray(transform.position, transform.TransformDirection(Vector3.right));
Ray rayLeft = new Ray(transform.position, transform.TransformDirection(Vector3.left));

RaycastHit wallImpactRight;
RaycastHit wallImpactLeft;

bool rightImpact = Physics.Raycast(rayRight.origin, rayRight.direction, out wallImpactRight, 0.45f);
bool leftImpact = Physics.Raycast(rayLeft.origin, rayLeft.direction, out wallImpactLeft, 0.45f);

if (Input.GetKey("w") && Input.GetAxis ("Horizontal") == 0f && rightImpact && Vector3.Angle
(transform.TransformDirection (Vector3.forward), wallImpactRight.normal) > 105f) {
//Debug.DrawRay (rayRight.origin, rayRight.direction * 10, Color.red);
rightWallRun = true;
startWallRun = 1;
return wallImpactRight;
}

else if (Input.GetKey("w") && Input.GetAxis ("Horizontal") == 0f && leftImpact && Vector3.Angle
(transform.TransformDirection(Vector3.forward), wallImpactLeft.normal) > 105f) {
wallImpactLeft.normal *= -1;

leftWallRun = true;
startWallRun = 1;
return wallImpactLeft;
}

else {
rightWallRun = false;
leftWallRun = false;
return new RaycastHit();
}
}

//right wall run start
if (rightWallRun) {
if (startWallRun >= 1 && canWallRun) {
if (Input.GetKey ("w") && Input.GetAxis ("Horizontal") == 0f) {
startWallRun = 2;
if (Input.GetButton ("Jump")) {
startWallRun = 3;
if (startWallRun >= 3) {
startWallRunMoveRight++;
startWallRun = 0;
}
}
}
}
}
//right wall run starts to move
if (startWallRunMoveRight >= 1){
Ray rayRight1 = new Ray(transform.position + new Vector3(0f,0.5f,0f), transform.TransformDirection(Vector3.forward + new Vector3(0.28f,0f,0f)));
RaycastHit wallImpactForward;
bool right = Physics.Raycast(rayRight1.origin, rayRight1.direction, out wallImpactForward, 1f);
Debug.DrawRay (rayRight1.origin, rayRight1.direction * 10, Color.red);
if (right){
transform.Rotate(0,-1,0);
Debug.DrawRay (rayRight1.origin, rayRight1.direction * 10, Color.green);
}
else {
right = false;
}
}

if (leftWallRun) {
if (startWallRun >= 1 && canWallRun) {
if (Input.GetKey ("w") && Input.GetAxis ("Horizontal") == 0f) {
startWallRun = 2;
if (Input.GetButton ("Jump")) {
startWallRun = 3;
if (startWallRun >= 3) {
startWallRunMoveLeft++;
startWallRun = 0;
}
}
}
}
}
//left wall run starts to move
if (startWallRunMoveLeft >= 1){
Ray rayLeft1 = new Ray(transform.position + new Vector3(0f,0.5f,0f), transform.TransformDirection(Vector3.forward + new Vector3(-0.27f,0f,0f)));
RaycastHit wallImpactForward;
bool left = Physics.Raycast(rayLeft1.origin, rayLeft1.direction, out wallImpactForward, 1f);
Debug.DrawRay (rayLeft1.origin, rayLeft1.direction * 10, Color.red);
if (left){
transform.Rotate(0,1,0);
Debug.DrawRay (rayLeft1.origin, rayLeft1.direction * 10, Color.green);
}
else {
left = false;
}
}
``````

your question is not very clear so that I can help you if possible can you explain it via diagram what you are up to …if you want your player to walk on the walls at whatever its inclined to then you have to update the gravity accordingly with the rotation .

I had this kind of problem in my game . what i did was I raycast and checked the inclination of the path that is coming in front of it and accordingly I updated the player gravity and the rotation … you can use the same logic …