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 Chickenator · Dec 18, 2014 at 06:52 PM · javascripttransform.lookat

How to do transform.LookAt() from a different part of object?

So basically I want to use transform.LookAt() with my sprite but it faces it in a way that it's impossible to see from 2D view.alt text

(don't ask about name of photo I couldn't be bothered to think of something to put)

I want it to face the target so you can see it. Heres my script btw

 #pragma strict
 
 var target : Transform;
 var bulletSpeed : float = 10;
 private var overallSpeed : float;
 
 function Update () {
 
 overallSpeed = bulletSpeed * Time.deltaTime;
     transform.LookAt (target);
         transform.position = Vector3.MoveTowards(transform.position, target.position, overallSpeed);
             
 }

Thanks In Advance

meowcat.jpg (28.6 kB)
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

1 Reply

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

Answer by HYPERSAVV · Dec 18, 2014 at 07:03 PM

Refer to the LookAt documentation. After pointing the forward vector at the desired position, it's rotating the object's up vector to the world up vector, which I'm assuming in your case is the issue. If the up vector on your object is facing the screen (z), it'll rotate it to the default which is (y) around the x-axis.

Comment
Add comment · Show 4 · 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 Chickenator · Dec 18, 2014 at 07:07 PM 0
Share

@isAndrewLong so what would the script look like

avatar image HYPERSAVV · Dec 18, 2014 at 07:29 PM 0
Share

When you call LookAt, pass in a second parameter to define what the up vector is. It sounds like you are trying to create a 2D game within a 3D environment and you want your sprites to exist in the y-plane. If this is so, then you're up vector is +z, and not +y (which is the default f you leave the second param blank).

avatar image Chickenator · Dec 18, 2014 at 07:31 PM 0
Share

@isAndrewLong I'm kinda new to Unity but I'm not making it in a 3d environment. I just went into 3d to show the rotation of the object. Also because I'm quite new, not much of what you said made much sense to me

avatar image HYPERSAVV · Dec 18, 2014 at 08:20 PM 0
Share

Hmm, let's try to break this down some more. What you are looking for is so that your object faces the other object, and what this means is that the forward vector of your object points in the direction of the other object. The Transform.forward documentation shows how you can find the angle necessary to rotate towards an object given your position and the position of the other object (see the second snippet). Once you have that angle, you can use Transform.Rotate to turn your object to face the second object.

I'm not sure how familiar you are with 3D $$anonymous$$ath, but it might be worth brushing up on it. Understanding 3D $$anonymous$$ath will help out a ton if you decide to stick with Unity. It's a bit much for this specific example, but this book is a great place to start.

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

27 People are following this question.

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

Related Questions

JS file deleting multiple game objects 0 Answers

JavaScript 3 Arrays questions 1 Answer

Is it the same javascript 1 Answer

Adding an offset to an instantiated objects rotation 0 Answers

Can I use Javascript (Unityscript) for Box2D for Unity or Farseer Physics? 1 Answer


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