Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by awplays49 · Sep 18, 2015 at 01:18 PM · physicscollidercharactercontrollercharacter controller

How to detect angles of walls?

I want to detect angles for some important features in my game. First, I want to develop a custom 3D character controller, but How can I make it so that, if I walk into a wall at 45 degrees for example, I move while rubbing on the wall but 2X slower? and the more I face the wall, the slower I go? And after 45 degrees or so, I can't move at all? Also, It would be helpful for custom physics. I want to make my own physics system that takes in physics material values and uses them in code. But, I don't know the formulas for how an object bounces off another. Can I do this with raycast? I just want to know what I can do to make this before I start creating my controller.

Comment
Add comment · Show 4
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 awplays49 · Sep 18, 2015 at 04:42 PM 0
Share

Something like This

avatar image awplays49 · Sep 18, 2015 at 05:38 PM 0
Share

I implemented some, heres the controller:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (PlayerControllerHandler))]
 public class PlayerController : $$anonymous$$onoBehaviour {
 
     public float gravity;
     public float moveSpeed;
     private Vector3 velocity;
     private float currentGravity;
     private PlayerControllerHandler pch;
     
     void Start () {
         pch = GetComponent <PlayerControllerHandler> ();
     }
     
     void Update () {
         currentGravity = velocity.y;
         velocity = Vector3.zero;
         velocity.y = currentGravity;
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W))
         {
             velocity += transform.forward;
         }
         else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
         {
             velocity -= transform.right;
         }
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D))
         {
             velocity += transform.right;
         }
         else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S))
         {
             velocity -= transform.forward;
         }
         velocity.y += gravity * Time.deltaTime;
         pch.$$anonymous$$ove (velocity * Time.deltaTime);
     }
 }

and heres the character controller handler:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerControllerHandler : $$anonymous$$onoBehaviour {
 
     public Layer$$anonymous$$ask collision$$anonymous$$ask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void $$anonymous$$ove (Vector3 velocity) {
         Collide (ref velocity);
         transform.Translate (velocity);
     }
     
     void Collide (ref Vector3 velocity) {
         RaycastHit hit;
         Vector3 point1 = transform.position + Vector3.down * cc.height / 4;
         Vector3 point2 = transform.position + Vector3.up * cc.height / 4;
         float rayDistance = Vector3.Distance (transform.position, transform.position + velocity);
         if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
         {
             Debug.Log ("Yes");
             if ($$anonymous$$athf.Abs (transform.position.x - hit.point.x) < $$anonymous$$athf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if ($$anonymous$$athf.Abs (transform.position.y - hit.point.y) < $$anonymous$$athf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if ($$anonymous$$athf.Abs (transform.position.z - hit.point.z) < $$anonymous$$athf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }
 
 
avatar image awplays49 · Sep 18, 2015 at 05:38 PM 0
Share

The only problem is the capsule cast isn't registering.

avatar image awplays49 · Sep 18, 2015 at 10:24 PM 0
Share

Here it is now, its somewhat working, but I bounce up higher and higher. Try this with the other script.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerControllerHandler : $$anonymous$$onoBehaviour {
 
     public Layer$$anonymous$$ask collision$$anonymous$$ask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void $$anonymous$$ove (Vector3 velocity) {
         Collide (ref velocity);
         transform.Translate (velocity);
     }
     
     void Collide (ref Vector3 velocity) {
         RaycastHit hit;
         Vector3 point1 = transform.position + Vector3.up * cc.height / 4;
         Vector3 point2 = transform.position + Vector3.up * cc.height * 0.75f;
         float rayDistance = Vector3.Distance (transform.position, transform.position + velocity);
         if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
         {
             Debug.Log (hit.point.y);
             if ($$anonymous$$athf.Abs (transform.position.x - hit.point.x) < $$anonymous$$athf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if ($$anonymous$$athf.Abs (transform.position.y - hit.point.y) < $$anonymous$$athf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if ($$anonymous$$athf.Abs (transform.position.z - hit.point.z) < $$anonymous$$athf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Sep 19, 2015 at 12:39 AM

You can do this with Raycast. The hit.normal on a simple object like a wall (without a normal map) should be all you need; see

http://answers.unity3d.com/questions/377506/how-i-can-know-surface-angle.html

Comment
Add comment · Show 1 · 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 awplays49 · Sep 19, 2015 at 02:46 AM 0
Share

Wow that's a helpful article! Thanks!

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

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Controller can pass through character controller after upgrade to 4.0 0 Answers

Character Controller, changing collider's size? 0 Answers

CharacterController Physics 1 Answer

Unity 4 to 5 Character Controller Issue 1 Answer

Player falls through floor after 10-20 seconds and above certain resolution consistantly(Bug?) 5 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