- Home /
align object forward with pipe
Hi there, i'm new to unity and to programming, i want to made a game where a ball run un a pipe, in the direction of the pipe, and rotate along the pipe longitudinals, i've succesfully aligned the up vector of the ball with the normal of the mesh through a raycast hit and quaternion rotation, it's possible to align the forward of the object with the texture y? Or with any other method?
var smoothTime : float = 0.4;
var speed : float = 500;
var gravity : float = 50;
var inputTranslate : boolean = false;
var inputPhisics : boolean = false;
private var velocity = Vector3.zero;
private var myNormal : Vector3;
private var adjustRotation : float = 90;
function Start()
{
myNormal = transform.up; // starting character normal
}
function Update ()
{
alignWithNormal();
if(inputTranslate)
inputTrans();
if(inputPhisics)
inputPhis();
}
function inputTrans ()
{
if (Input.GetKey ("up"))
transform.Translate( Vector3.forward * speed * Time.deltaTime, Space.Self);
if (Input.GetKey ("down"))
transform.Translate( -Vector3.forward * speed * Time.deltaTime, Space.Self);
if (Input.GetKey ("left"))
transform.Translate( Vector3.left * speed * Time.deltaTime, Space.Self);
if (Input.GetKey ("right"))
transform.Translate( Vector3.right * speed * Time.deltaTime, Space.Self);
}
function inputPhis ()
{
if (Input.GetKey("up"))
rigidbody.AddTorque(transform.forward * speed );
if (Input.GetKey("down"))
rigidbody.AddTorque(-Vector3.forward * speed );
if (Input.GetKey("left"))
rigidbody.AddTorque(Vector3.left * speed );
if (Input.GetKey("right"))
rigidbody.AddTorque(Vector3.right * speed );
}
function alignWithNormal ()
{
var hit : RaycastHit;
var ray: Ray;
var angle : float;
var angleCorrection : float;
ray = Ray(transform.position, -myNormal);
if (Physics.Raycast (ray , hit, 10))
{
myNormal = Vector3.Lerp(myNormal, hit.normal, Time.deltaTime);
rigidbody.AddForce(-hit.normal*gravity);
var textureCord = hit.textureCoord;
print(textureCord.y);
}
// rotate character to myNormal...
var rot = Quaternion.FromToRotation(Vector3.up, myNormal);
transform.rotation = rot;
}
Well, raycastHit returns the texture coordinate, so if you saved that between frames you could work out your yVelocity, and rotate towards that! Otherwise, just rotate towards the direction you are currently moving.
I know that, but i can't figure it out, and i have another problem, when i add. torque forward the ball move left, and when i add torque right with the controls, the ball move forward, i dont know why?
Hi syclamoth, can you do an example with the code above to orient the forward to the texture.y? Thanks.
Your answer

Follow this Question
Related Questions
Unity "Cleaning up leaked objects" on save scene 0 Answers
Truck moves sideways when using transform.forward 1 Answer
Object space normal map 0 Answers
How get object, if i know coordinates? 2 Answers