I am making a platformer, and I was wondering to make it possible to detect a collision only on one side of an object. You see, when I jump and I hit a platform on the side I can also “wall jump” and I don’t want that. Is there a(n easy) way to detect collision on one side? (Preferabely in C#)
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody rb;
public float Speed = 25f;
public float JumpForce = 50f;
private bool CanJump = false;
// Update is called once per frame
void FixedUpdate () {
if ( Input.GetKey("right") )
{
rb.AddForce(Speed, 0, 0);
}
if ( Input.GetKey("left") )
{
rb.AddForce(-Speed, 0, 0);
}
if ( Input.GetKey("space") )
{
Jump ();
}
}
void Jump(){
if (CanJump) {
CanJump = false;
rb.AddForce(0, JumpForce, 0);
}
}
void OnCollisionEnter (Collision collidingObject) {
if (collidingObject.gameObject.name == "Platform") {
CanJump = true;
}
}
}
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.InputField.html There are in Canvas->UI the InputField Text there are automatic input text you have just to script to create a variable that contain all the texts of your fields writing a single string with a for loop and after you can convert the string with type.Parse if you need a specif typologies of "IDs", and the game is done.
It might be easier to just use a trigger collider to provide a jump, covering only that one side.
That said, you can get the collision points for any collision, so you can check the outward facing surface direction for each contact point. In the event you wanted to check that someone touched the “top” of your platform, you might try something like this:
private Vector3 validDirection = Vector3.up; // What you consider to be upwards
private float contactThreshold = 30; // Acceptable difference in degrees
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Platform")
{
for (int k=0; k < col.contacts.Length; k++)
{
if (Vector3.Angle(col.contacts[k].normal, validDirection) <= contactThreshold)
{
// Collided with a surface facing mostly upwards
CanJump = true;
break;
}
}
}
}
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.InputField.html There are in Canvas->UI the InputField Text there are automatic input text you have just to script to create a variable that contain all the texts of your fields writing a single string with a for loop and after you can convert the string with type.Parse if you need a specif typologies of "IDs", and the game is done.
– Venividiviciuus