Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Useless-Media · May 05, 2015 at 05:01 AM · rotationraycastwalls

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wenbo · May 05, 2015 at 09:57 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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]
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges