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 DezC · Aug 02, 2012 at 09:09 AM · lookatspherecylinder

LookAt function not working properly(cylinder looking at sphere)

I'm currently having difficulties letting the one end of the cylinder face a sphere.

I already created a script of:

 //---------------------------------
 
 var target: Transform;
 
 function Update () 
 {
     transform.LookAt(target);
 }
 
 //-----------------------------

I've set the sphere as the target but the side/body of the cylinder is the one facing the sphere. How do I make the one end of the cylinder face the sphere instead of its side/body?

Help please

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

3 Replies

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

Answer by Bunny83 · Aug 02, 2012 at 09:28 AM

A common solution is to add your cylinder as a child to an empty gameobject. Set your cylinders position inside the empty GO to 0,0,0 and your rotation to your desired rotation, for example 90,0,0

Now attach your sctipt to the empty GameObject and it should work. The script still aligns the z-axis towards the target, but the z-axis of the empty GameObject. Since the cylinder is a child it will move and rotate along, but is pre-rotated by 90°.

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
avatar image
1

Answer by whydoidoit · Aug 02, 2012 at 09:17 AM

You can do this:

      transform.up = (target.position - transform.position);

But that can mess with the other rotations.

      transform.LookAt(target);
      transform.Rotate(Vector3.right, 90);
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
avatar image
0

Answer by cupsster · Aug 02, 2012 at 09:18 AM

     public Transform target;
     void Update() {
         transform.LookAt(target, Vector3.up);
     }

Also try to replace Vector3.up with Vector3.right up or forward and you can also play with adding minus - sign in front of them if you want oposite variant of them.

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 02, 2012 at 09:25 AM 0
Share

That doesn't change the direction the object will be facing. The objects z-axis will always point at the target. The upvector just specifies the rotation around that target vector.

Also Vector3.up is a static "constant". The new keyword can only be used with an objects constructor. so either remove the "new", or use something like this:

 new Vector3(0,1,0)

which is the same as:

 Vector3.up
avatar image cupsster · Aug 02, 2012 at 09:32 AM 0
Share

you are right Bunny

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting two spheres with a cylinder 1 Answer

how add cylinder with rigid body inside a sphere with rigid body? 0 Answers

How to make a cylinder follow two sphere 1 Answer

Move Spheres In a Hollow Cylinder 1 Answer

Problems with an object rotating a sphere surface (Quaternions and LookAt) 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