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 valtteri_m · Apr 20, 2015 at 04:31 PM · camerarotationjavascriptupdatelookat

Look at wont update?

so I got this script for my camera, it makes so that an object (target) rotates so that its looking to the same way i'm looking when I press one of the WASD keys, the problem I got is that it only does it once, help??

 #pragma strict
 
 var target : Transform;
 var Distance = 10;
   
 function Update(){
     if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
         target.LookAt(Vector3(transform.position.x - 180,target.position.y,target.position.z));
     }
     transform.position = target.position - transform.forward * Distance;
 }
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

2 Replies

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

Answer by Eno-Khaon · Apr 20, 2015 at 08:15 PM

Maybe I'm simply not understanding the intent, but why is the target looking 180 units away from the camera on the X axis, but towards itself on the Y and Z?

If the intent is to rotate the effective X axis by 180 degrees, you wouldn't want to do that with the target direction you're getting this object to face. If you want a simple "face the same way as the camera, but straight along the ground" sort of thing, try:

 target.EulerAngles = Vector3(target.EulerAngles.x, transform.EulerAngles.y, target.EulerAngles.z);

With this, you could also change the Y axis to (transform.EulerAngles.y + 180) as desired.

Comment
Add comment · Show 1 · 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 valtteri_m · Apr 21, 2015 at 01:33 PM 0
Share

Yeah I realized this my self later on, and your script line works like a charm!

what I had going on for me was

target.transform.rotation.y = transform.rotation.y;

but it sometimes bugged out when I did 180 turns, but yours doesn't, thanks!

avatar image
0

Answer by Spinnernicholas · Apr 20, 2015 at 04:44 PM

You have to setup the "Vertical" and "Horizontal" axis in the Input Manager before you can use them.

Also, Input.GetAxis returns a float between -1 and 1, representing the axis position. It does not return a boolean for a logical condition statement. You should instead check if either of them is == to 0.

 if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){
 //....  
 }

Comment
Add comment · Show 5 · 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 valtteri_m · Apr 20, 2015 at 05:45 PM 0
Share

I have setup them, I tried the !=0 but it still only does it once and after that it wont do it..

avatar image Spinnernicholas · Apr 20, 2015 at 06:49 PM 0
Share

If you want it to look at the point at "Distance" in front of this object, try this:

 target.LookAt(transform.position + transform.forward * Distance);

You start with this object initial position, and then add it's forward vector times the distance.

avatar image rockyourteeth · Apr 20, 2015 at 07:26 PM 0
Share

I haven't done it myself, but it looks like using his Boolean logic would actually work, because if either of those values are non-zero, then it should cast to a bool and return true.

avatar image Spinnernicholas · Apr 20, 2015 at 08:21 PM 0
Share

Yeah, it will work, but it is really bad code. It hinder's the "readability" of the code and hinders "reusability" because it is ambiguous. Stuff like that is a big reason coding is so difficult. Whoever wrote this probably did it to confuse beginning programmers. If you do want to use the shortcut, add a comment above that line explaining the purpose. eg:

 //if joysticks are active
avatar image Spinnernicholas · Apr 20, 2015 at 08:22 PM 0
Share

Did you try the modified target.LookAt() call?

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

21 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

Related Questions

How Can I Stop Rotation From This Script? 0 Answers

Demons Souls Fall death Camera 1 Answer

I need help with camera movement and rotation. 1 Answer

"Perfect" Free Rotation Method? (like Blender) 2 Answers

Move an object toward an angle in 2d space 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