Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by julienb · Dec 03, 2011 at 01:40 AM · rotationquaternionsx

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.

alt text

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...

http://pastebin.com/i4jMJGnH

 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);    
     
     
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
 ...

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image julienb ♦♦ · Dec 04, 2011 at 01:59 PM 0
Share

Thanks ! That is really how it should be done !

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Rotate object instead of camera around it? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges