- Home /
align to floor in local X
I am trying to make a snowboard game just for fun. I am no programmer, I am just some one playing with Unity. I am trying to align the board only on local x to the ground. It aligns well when I start the script but as soon as I make the board rotate it acts very weird. I am very bad with quaternions :(. If you know a great reference to learn quaternions and math physics for games please let me know ! The scene is a blue board with the following script applied to it and a floor.
Here is my code, I know there is A LOT to improve. But the orientation to ground part is what bugs me the most right now...
using UnityEngine;
using System.Collections;
public class boardSim : MonoBehaviour {
public float airFriction=1f;
public float snowFriction=4f;
public float gravity=-1.8f;
public bool isGrounded=true;
public float upSpeed=0f;
public float groundDistance;
public float speed;
public float friction;
public GameObject raycaster;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//gravity on the board
RaycastHit gravityHit;
Physics.Raycast(transform.position, -Vector3.up, out gravityHit);
groundDistance=gravityHit.distance;
//orientation to ground
RaycastHit hit;
Physics.Raycast(transform.position, -Vector3.up, out hit);
Quaternion hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
// the last line makes no sense, but it could be translated to:
Quaternion rot = transform.rotation; // copy rotation to an aux variable...
rot.x = hitRotation.x; // change the x component in that variable...
//rot.z = hitRotation.z; // change the x component in that variable...
transform.localRotation = rot; // then assign it to rotation
if (groundDistance>0.2){
upSpeed=gravity;
friction=airFriction;
isGrounded=false;
}
else{
upSpeed=0f;
friction=snowFriction;
isGrounded=true;
}
//speed calculation of the board
if (Input.GetButton("boardMotor")){
speed+=5f*Time.deltaTime;
}
else{
speed-=friction*Time.deltaTime;
}
speed=Mathf.Clamp(speed,0,100);
//edging of the board
Debug.Log(Input.GetAxis("boardEdge"));
transform.Rotate(Vector3.up, Time.deltaTime*Input.GetAxis("boardEdge")*100);
//applying translation
transform.Translate(0, upSpeed*Time.deltaTime, speed*Time.deltaTime, transform);
}
}
Answer by aldonaletto · Dec 03, 2011 at 06:45 PM
There's a smart way to align only your local X rotation to the ground normal - it's a trick used by @SirGive in this question. The basic idea is to do two Raycasts to the ground - one from the snowboard front, the other from the snowboard back. Subtracting the back hit.point from the front hit.point you get a vector parallel to the ground, and pointing forward; set transform.forward to this vector and presto! your snowboard will follow the ground inclination only in the local YZ plane (around the X axis), and without loosing the direction it's turned to:
public float offset = 0.5f;
void Update () {
RaycastHit frontHit;
RaycastHit backHit;
Vector3 pos = transform.position;
Physics.Raycast(pos + offset * transform.forward, -Vector3.up, out frontHit);
Physics.Raycast(pos - offset * transform.forward, -Vector3.up, out backHit);
transform.forward = frontHit.point - backHit.point;
groundDistance = (frontHit.distance + backHit.distance)/ 2; // get the average distance
if (groundDistance>0.2){
upSpeed=gravity;
friction=airFriction;
isGrounded=false;
}
else{
upSpeed=0f;
friction=snowFriction;
isGrounded=true;
}
...
}
Answer by syclamoth · Dec 03, 2011 at 09:03 AM
You shouldn't be rotating the object from the scene view if you expect it to act in any sane manner. That happens outside of your scripts, and can interfere with their proper action.
In any case, the main issue you are having here is that you are attempting to modify the direct xyzw values of a quaternion. Unfortunately, Quaternions are basically weird. I don't fully understand them myself, but the very clever people at Unity who wrote the Quaternion class do, and have provided you with a whole set of methods for manipulating and creating them!
Assuming that the rest of your movement code works, rotating the board to the direction it is moving in, here's how I'd start with the rotation to floor bit-
RaycastHit hit;
Physics.Raycast(transform.position, -Vector3.up, out hit);
Vector3 oldAxis;
float angle;
transform.rotation.ToAngleAxis(out angle, out oldAxis);
Quaternion newAngle = Quaternion.AngleAxis(angle, hit.normal);
transform.rotation = newAngle;
Your answer
Follow this Question
Related Questions
How can I instantiate a gameobject facing another gameobject 2D? 0 Answers
local rotation per axis between frames? 1 Answer
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer
Quaternions; how do I rotate an object around only 2 axis based on another rotation? 1 Answer