- Home /
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
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?
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);
}
}
Answer by huh12 · Mar 25 at 12:45 AM
Thank u so much!
If that was the solution, would you mind accepting it as an answer? :)
Your answer
Follow this Question
Related Questions
Make an FPS gun point at the mouse position. 2 Answers
LookAt but using physics AddRelativeTorque 1 Answer
Lookat mouse, without raycasting? 1 Answer
How to send vars! 1 Answer
Autorotation to tween gameobject 0 Answers