- Home /
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.
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;
}
}
}
}
The only problem is the capsule cast isn't registering.
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;
}
}
}
}
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
Your answer
Follow this Question
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