- Home /
Question by
EDAC-Student · Jun 12, 2017 at 10:16 PM ·
physics2dplatformerangle adjust
How do I orient a 2D character to the ground/obstacles normal?
I am creating a 2D game, where the character must climb over obstacles etc to reach the end goal. However, he stays "upright" the entire time and does not look like he is walking at an angle on an angled surface. I do not want to use the generic physics, so I have locked the rotations.
Any and all help will be greatly appreciated.
I have is some-what working however, but it is not good enough. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
[SerializeField] float distance;
[SerializeField] LayerMask layerMask;
Quaternion targetRotation;
public float rotationMultiplier;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.position, -(transform.up), distance, layerMask);
Debug.DrawLine(transform.position, transform.position - (transform.up) * distance);
//U.x*V.y-U.y*V.x)
if (hit.transform)
{
float rot = hit.transform.rotation.eulerAngles.z;
rot = Mathf.Clamp(rot, -45, 45);
targetRotation = Quaternion.Euler(0, 0, rot);
print(rot);
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationMultiplier);
}
}
Comment