Quantcast
Channel: Answers by "Foose"
Viewing all articles
Browse latest Browse all 34

Answer by Foose

$
0
0
Hey. I don't know if you actually handled your problem now because it's been a while since you have asked. I pretty much had the same problem and I have to say I am a programming noob, but I finally found a solution even if it is a bit unorthodox and every programmer would hang me for that :D So first of all, let's say all blocks and your character are rigidbodies. You will need two kinds of script. One for your character that handles the movement and one for the blocks that handles the collision. playerMovement.cs` using System.Collections; public class playerMovement : MonoBehaviour { public int speed = 8; KeyCode LeftArrow; KeyCode RightArrow; KeyCode UpArrow; KeyCode DownArrow; // Update is called once per frame void FixedUpdate () { if (Input.GetKeyDown (KeyCode.LeftArrow) && rigidbody.isKinematic == true){ rigidbody.isKinematic = false; rigidbody.velocity = new Vector3(0, 0, speed); rigidbody.angularDrag = 1; } if (Input.GetKeyDown (KeyCode.RightArrow) && rigidbody.isKinematic == true){ rigidbody.isKinematic = false; rigidbody.velocity = new Vector3(0, 0, -speed); rigidbody.angularDrag = 2; } if (Input.GetKeyDown (KeyCode.UpArrow) && rigidbody.isKinematic == true){ rigidbody.isKinematic = false; rigidbody.velocity = new Vector3(speed, 0, 0); rigidbody.angularDrag = 3; } if (Input.GetKeyDown (KeyCode.DownArrow) && rigidbody.isKinematic == true){ rigidbody.isKinematic = false; rigidbody.velocity = new Vector3(-speed, 0, 0); rigidbody.angularDrag = 4; } } }` This one will handle the playerMovement and has a very unique clue. I set up a very low angular drag at the start of each movement to detect what kind of movement the player is doing currently. Now it is easy for the collider to know which kind of movement. So you have no problem with collision when the player is moving along a wall. Normally the collision detection would trigger then too. But not now. Next thing you have to do is write a collision detection script for the blocks. In my case I made 4. One for the upper wall, one for the left, and so on. So this is the example for the left one. ObjectCollisionLeft.cs `using UnityEngine; using System.Collections; public class ObjectCollisionLeft : MonoBehaviour { void OnCollisionEnter ( Collision collision) { if (collision.gameObject.CompareTag("Player") && collision.rigidbody.angularDrag == 1) { collision.rigidbody.isKinematic = true; } } }` I hope this example will help you as much with your problem as it did with mine :) have a nice day.

Viewing all articles
Browse latest Browse all 34

Latest Images

Trending Articles



Latest Images