- Home /
problem with vector3.Angle with a fixed joint
i need measure angle of a rigidbody conected to a fixed joint, but inside the simulation the results are incorrect. i am using vector3.angle.
i need simulate a pendulum and measure the angle of the pendulum, any help? i include the project
this is the code:
using UnityEngine;
using System.Collections;
public class angulo : MonoBehaviour {
public Transform target;
public float anguloMedida;
public enum direccion{x,y,z}
public direccion direccionActual = direccion.x;
Vector3 valor;
void Update()
{
switch (direccionActual)
{
case direccion.x:
valor = transform.right;
break;
case direccion.y:
valor = transform.up;
break;
case direccion.z:
valor = transform.forward;
break;
}
Vector3 targetDir = target.position - transform.position;
Vector3 forward = transform.forward;
float anguloMedida = Vector3.Angle(targetDir, valor);
print (anguloMedida);
}
}
Probably some of the points you measure from are wrong, or arrows are backwards... . Finding the pivot point from the Joint is a pain -- it's in local space coords (I think.) If you draw a picture and label things, the problem often becomes clear.
Then, the other most popular comment: we can't see your code, so can't know what's wrong.
Answer by Owen-Reynolds · Apr 07, 2014 at 10:34 PM
I don't understand those numbers, but maybe using transform.forward
is the problem?
transform.forward
is your personal forward, the blue arrow. It spins as the pivot spins, so they cancel. It's like driving a car in a circle while checking if the passenger is on your left or behind you.
Seems like there should be one fixed reference, such as comparing to up: Angle(Vector3.up, target.position - transform.position).
Answer by wicked208 · Apr 08, 2014 at 07:10 AM
Oh my god, its fixed now, you are right, i have to use vector3.up, i copy the code from the script reference they should change this, anyway thanks very much.