- Home /
Rotating based on the platform the player is standing on?
I am trying to make a 2d skateboarding game and I need to make the player rotate based on the rotation of the block under neath the player. This way skating down a ramp the board is on the ground
(I am kind of new and this is how I would think of it) Basically I was thinking of casting a ray or somthing that would detect the platform underneath the player. Then it would then find the rotation of that platform and rotate the player perpendicular to that.
Does anyone know how to find the rotation of an object underneath a player object?
comment don't make that an answer.
cast a ray from player.transform down.
take the inverse(-) of the normal of the surface you hit use those to find the axis of rotation and the angle, I know you've no idea how to find those with just that information so i've posted the code.
axis = vector3.cross(-player.transform.up,-hit.normal);
if(axis != vector3.zero)
angle = atan2(vector3.magnitude(axis), vector3.dot(-player.transform.up,-hit.normal));
player.transform.rota$$anonymous$$round(axis,angle);
this will rotate the character so his feet are always parallel with the ground. mark as answered and have a nice day
THAN$$anonymous$$ YOUUU...this works great. Just one tinny little issue. Would i be able to rotate the player slowly when its is moving slowly against a curvy platform? it rotate very quick and sharp atm if you can understand what im tryna say??
use slerp to rotate over time ins$$anonymous$$d of instantly.
Thank you for replying and ya sorry about submitting that as a an anwser. I don't have time to look at it today because I have tests to study for, but once I look at it and if it works I will make it the anwser.
Answer by mkjrfan · Jan 25, 2013 at 09:06 PM
No idea why but for some reason sparkzbarca's anwser became a comment and now neither I nor anyone else can see the rest of the conversation even if you coment on it. This is what he wrote as the anwser: Ray ray = new Ray(); RaycastHit hit; Vector3 axis; float angle;
ray.origin = transform.position;
ray.direction = -transform.up;
Physics.Raycast(ray, out hit);
axis = Vector3.Cross(-transform.up,-hit.normal);
if(axis != Vector3.zero)
{
angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up,-hit.normal));
transform.RotateAround(axis,angle);
}
it works.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Collide : $$anonymous$$onoBehaviour { Ray ray = new Ray(); RaycastHit hit; float angle; public bool isTurning; Quaternion pastRot; float rotX; float rotY; float rotZ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
pastRot = this.transform.rotation;
ray.origin = transform.position;
ray.direction = -transform.up;
Physics.Raycast(ray, out hit);
rotX = hit.transform.rotation.x - transform.rotation.x;
rotZ = hit.transform.rotation.z - transform.rotation.z;
this.transform.rotation *= Quaternion.Euler(rotX * 100 * Time.deltaTime, rotY * 100 * Time.deltaTime, rotZ * 100 * Time.deltaTime);
transform.parent = hit.transform;
if (this.transform.rotation != pastRot)
{
isTurning = true;
}
if (this.transform.rotation == pastRot)
{
isTurning = false;
}
}
}
This is my code. I used sparkzbarcas and it ended up rotating on the y axis if the x and z axis were both non-zero values for me.
Answer by nantoaqui · Dec 12, 2017 at 11:22 PM
Hello!!
Works perfectly! I would like to know why are we using Vector3.Dot in this case to get the x position?
Thank you!