- Home /
adjust the player in the ground And movements
Hi
how to catch the 'direction' of the vertex/polygon to apply to the character? the point of it be adjusted to the ground.
Exemple:
the ground in green is only a picture with component polygon collider.
Question 2:
using a simple script that uses rigidbody2d to move the player,I found some problems: using rigidbody the player slide When it gets on a mountain. to climb a mountain is more difficult than to descend. is there any alternative for this? or the rigidbody2d? ("put the seriousness of the player in the direction of the object could solve this")
I have done this before. But you have to forget about using unity's physics here.
--Raycast from the player towards the collider.
--Get the colliders perpendicular normal.
--From here the perpendicular normal should be adjusted to the correct player movement direction.
--And finally rotate the character and move it accordingly to the correct perpendicular normal vector.
I can post you a code example later tonight, because i just don't have enough time now. But by that time maybe somebody will post you a code sample as an answer.
Answer by jenci1990 · Dec 04, 2014 at 09:59 AM
//C#
void OnCollisionStay (Collision col) {
if (col.collider.gameObject.tag == "Ground") { //Check the collider is ground
transform.up = col.contacts[0].normal;
}
}
//JS
function OnCollisionStay (col : Collision) {
if (col.collider.gameObject.tag == "Ground") { //Check the collider is ground
transform.up = col.contacts[0].normal;
}
}
Answer by sysameca · Dec 05, 2014 at 08:33 AM
Ok. I made a short example of what you are trying to achieve. What you need is a ground with some 2D collider and a Player object with this script only. I still don't have enough time to polish it, but you will get the picture:
using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
private void Update()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.down, Mathf.Infinity);
// Get the perpendicular normal
Vector3 direction = new Vector3(hit.normal.y * -1, hit.normal.x);
// Move right
if (Input.GetKey(KeyCode.D))
{
transform.position += direction * -1 * Time.deltaTime;
transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
transform.rotation = Quaternion.LookRotation(Vector3.forward, hit.normal);
}
// Move left
else if (Input.GetKey(KeyCode.A))
{
transform.position += direction * Time.deltaTime;
transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
transform.rotation = Quaternion.LookRotation(Vector3.forward, hit.normal);
}
}
}
This example does not rely on physics therefore you need to implement your own rules for gravity and other stuff, but the result can be observed when you hit play. In real good platformer kind of game it's good to set your own rules even if it's harder at first.
I tested your script, but it is so: http://somevid.com/0AL$$anonymous$$eQnBJdCj$$anonymous$$nlv8BGV
....
Yes indeed the rotation was not correct. I was rushing in this morning. Check it out now it is reupdated.