Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

zxy angles as in quaternian

Discussion in 'Physics' started by monsterbuzz786, Dec 4, 2016.

  1. monsterbuzz786

    monsterbuzz786

    Joined:
    Oct 17, 2016
    Posts:
    22
    I'm trying to rotate a model to align parallel with the road. I have some code that keeps showing perpendicular angles.

    i'm not sure if i have the right angles calculated.
    Code (csharp):
    1.  
    2. //rotate to road angle
    3.         RaycastHit hit;
    4.         Physics.Raycast(transform.position, transform.up, out hit, 20f);
    5.  
    6.         float xy = Vector2.Angle(new Vector2(hit.point.x, hit.point.y), new Vector2(transform.position.x, transform.position.y));
    7.         float xz = Vector2.Angle(new Vector2(hit.point.x, hit.point.z), new Vector2(transform.position.x, transform.position.z));
    8.         float yz = Vector2.Angle(new Vector2(hit.point.y, hit.point.z), new Vector2(transform.position.y, transform.position.z));
    9.  
    10.         gameObject.GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(new Vector3(xy, yz, xz)));
     
    Last edited: Dec 4, 2016
  2. monsterbuzz786

    monsterbuzz786

    Joined:
    Oct 17, 2016
    Posts:
    22
    heres the full code...

    I want the model to align with the ground transform. Also my model doesn't turn with turnTorque.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class carController : MonoBehaviour {
    7.     public float acceleration;
    8.     public float rotationRate;
    9.  
    10.     public float turnRotationAngle;
    11.     public float turnRotationSeekSpeed;
    12.  
    13.     private float rotationVelocity;
    14.     private float groundAngleVelocity;
    15.     private float ROTvel;
    16.     private float rotvel2;
    17.     void Update(){
    18.     }
    19.     // Update is called once per frame
    20.     void FixedUpdate () {
    21.  
    22.         //forward Force
    23.         Vector3 forwardForce = transform.forward * acceleration * Input.GetAxis("Vertical");
    24.         forwardForce = forwardForce * Time.deltaTime * gameObject.GetComponent<Rigidbody>().mass;
    25.         gameObject.GetComponent<Rigidbody>().AddForce(forwardForce, ForceMode.Force);
    26.         //GameObject.Find("engine").GetComponent<Rigidbody>().AddForce(forwardForce, ForceMode.Force);
    27.  
    28.         //turn Velocity
    29.         Vector3 newRotation = transform.rotation.eulerAngles;
    30.         newRotation.y = Mathf.SmoothDampAngle(newRotation.y, Input.GetAxis("Horizontal")*turnRotationAngle, ref rotationVelocity, turnRotationSeekSpeed);
    31.  
    32.         //rotate to road angle
    33.         RaycastHit hit;
    34.         Physics.Raycast(transform.position, transform.up, out hit, 20f);
    35.         Vector3 hitn = hit.normal;
    36.         float hitx = Vector3.Angle(new Vector3(0,hit.collider.transform.rotation.eulerAngles.y,0), new Vector3(0,0,hit.collider.transform.rotation.eulerAngles.z));
    37.         float hity = Vector3.Angle(new Vector3(hit.collider.transform.rotation.eulerAngles.x,0,0), new Vector3(0,0,hit.collider.transform.rotation.eulerAngles.z));
    38.         float hitz = Vector3.Angle(new Vector3(0,hit.collider.transform.rotation.eulerAngles.y,0), new Vector3(hit.collider.transform.rotation.eulerAngles.x,0,0));
    39.         //gameObject.GetComponent<Rigidbody>().AddTorque(new Vector3(hitn.x*transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, hitn.z*transform.rotation.eulerAngles.z));
    40.  
    41.         //Turn Model
    42.         Vector3 turnTorque = Vector3.up * rotationRate * Input.GetAxis("Horizontal");
    43.         turnTorque = turnTorque * Time.deltaTime * gameObject.GetComponent<Rigidbody>().mass;
    44.         gameObject.GetComponent<Rigidbody>().AddTorque(turnTorque);
    45.     }
    46. }
    47.  
    48.  
     
    Last edited: Dec 4, 2016