Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by huh12 · Mar 24 at 01:34 PM · lookat

I created a LookAt function but the camera does not look at the car that I'm using. BTW I'm following a tutorial

This is the code I wrote:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraThing : MonoBehaviour
 {
     public float input;
 
     public GameObject player;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         input = Input.GetAxis("Horizontal");
 
         gameObject.transform.localPosition = new Vector3(gameObject.transform.localPosition.x + input, gameObject.transform.localPosition.y , gameObject.transform.localPosition.z);
         gameObject.transform.LookAt(player.transform.position);
     }
 }
 

If you notice anything wrong pls tell me. thx

Comment
Add comment · Show 1
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 Captain_Pineapple · Mar 24 at 04:16 PM 0
Share

To solve an error it is best to understand what actually goes wrong. For that it is best to know what happens instead of the behaviour that you expect.


That being said: What happens? Where does the camera look instead? Is there any error in the console?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Narc0t1CYM · Mar 24 at 07:52 PM

I'm not sure if this is what you wanted to achieve, but I think the issue was, that you were using transform.localPosition instead of transform.position. Also, you don't need gameObject when you want to reference transform, you can use them separately. You were also using FixedUpdate - which is for physics - instead of LateUpdate - which runs after everything finished in all other XY Updatemethods


I tested your script and for me pushing A and D (or left and right arrow), will make the camera zoom in and out on Player. Here's the corrected script

 using UnityEngine;
  
 public class CameraThing : MonoBehaviour
 {
     public float input;
  
     public GameObject player;
  
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
  
     // Update is called once per frame
     void LateUpdate()
     {
         input = Input.GetAxis("Horizontal");
  
         transform.position = new Vector3(transform.position.x + input, transform.position.y , transform.position.z);
         transform.LookAt(player.transform.position);
     }
 }



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 huh12 · Mar 25 at 12:45 AM

Thank u so much!

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 Narc0t1CYM · Mar 25 at 04:07 AM 0
Share

If that was the solution, would you mind accepting it as an answer? :)

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

135 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 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 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 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 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

Respawn looking 1 Answer

Aim flashlight at cursor in third person 1 Answer

how do i make somthing rotate with mouse 1 Answer

Slow LookAt 1 Answer

Missile Homing and correcting the "dead zone" 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