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
0
Question by GTONE · Oct 09, 2011 at 10:28 PM · objecttexture2dforwardnormalvector3.up

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

alt text

Comment
Add comment · Show 6
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 syclamoth · Oct 09, 2011 at 10:38 PM 0
Share

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.

avatar image GTONE · Oct 10, 2011 at 10:59 AM 0
Share

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?

avatar image syclamoth · Oct 10, 2011 at 11:58 AM 0
Share

Adding torque creates angular velocity! That'd be why.

avatar image GTONE · Oct 12, 2011 at 08:42 PM 0
Share

Hi syclamoth, can you do an example with the code above to orient the forward to the texture.y? Thanks.

avatar image StevenNieh · Feb 08, 2012 at 05:13 PM 0
Share

Yes, please show an example. Thanks!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity "Cleaning up leaked objects" on save scene 0 Answers

Truck moves sideways when using transform.forward 1 Answer

How place object in forward direction of the camera, even if camera is pointing to the ground or the sky? 2 Answers

Object space normal map 0 Answers

How get object, if i know coordinates? 2 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