- Home /
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
. .
[Code=Csharp]
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...
}[/code]
Please help me i realy dont get it now!
PS: If some need a raycast to detect walls to finaly wall run, this one works fine for me.
I think,you should try to abstract you problem to a simple model,which is short for viewers to look through and the most import is to make it a nuclear problem but not the whole problem,you can not solve your problem just because you can not solve the key problem in the whole problem,so the key problem counts.
Answer by Useless-Media · May 05, 2015 at 05:43 PM
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:
[Code=Csharp]
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;
}
}[/code]
Answer by alok-kr-029 · May 05, 2015 at 10:14 AM
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 ...
Your answer
Follow this Question
Related Questions
How does LookRotation work in relation to ray cast? 1 Answer
Unity Rotate Raycast on Quaternion 1 Answer
Raycasting from object rotation? 1 Answer
click to move workig script!! but pls help with rotation!! :( 1 Answer
How do I get an object to rotate around a different axis based on the raycasts hit angle? 0 Answers