Search Unity

Cannot Follow the object properly with camera

Discussion in 'Animation' started by israfiluzuk, Oct 8, 2019.

  1. israfiluzuk

    israfiluzuk

    Joined:
    Oct 4, 2019
    Posts:
    1
    Hello everyone. I am really new at UNITY. I am trying to make a Marble Race game. I am trying follow the Player's ball by camera. But camera does not turn by road.
    I want the camera to replace by road.

    here is the video and the code.

    PlayerControl.cs
    using PathCreation;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class PlayerController : MonoBehaviour {

    public float ballSpeed;
    public GameObject Road1;

    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    transform.rotation = Quaternion.Euler(0,0,0);
    }

    //Physics Codes
    void Update()
    {
    //Controlling vertical and horizontal
    float moveVertical = Input.GetAxis("Vertical");
    float moveHorizontal = Input.GetAxis("Horizontal");


    Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);

    rb.AddForce(movement*ballSpeed);
    }

    }


    --------------------------------------------------------
    CameraController.cs

    using PathCreation;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraController : MonoBehaviour {

    public GameObject player;
    //public GameObject Road1;
    private Vector3 offset;

    // Use this for initialization
    void Start () {
    offset = transform.position - player.transform.position;

    }

    void Update()
    {
    transform.position = player.transform.position + offset;

    }
    }