Hello i am new to unity and still fighting with basics…
So in m game i see my game object Flickers as it moves in straight line.
I have tried Fixed Update or *Time.deltaTime with no results.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour
{
public static bool LevelSelected;
public static float speed;
[SerializeField]
bool started;
bool gameOver;
Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Use this for initialization
void Start()
{
started = false;
LevelSelected = false;
gameOver = false;
}
// Update is called once per frame
void Update()
{
if (LevelSelected)
{
if (!started)
{
if (Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector3(speed, 0, 0);
started = true;
GameManager.instance.StartGame();
}
}
}
if (Input.GetMouseButtonDown(0) && !gameOver)
{
SwitchDirection();
}
}
void SwitchDirection()
{
if (rb.velocity.z > 0)
{
rb.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self);
rb.velocity = new Vector3(speed, 0, 0);
}
else if (rb.velocity.x > 0)
{
rb.transform.Rotate(0.0f, -90.0f, 0.0f, Space.Self);
rb.velocity = new Vector3(0, 0, speed);
}
}
public void StopMoving()
{
rb.useGravity = true;
rb.isKinematic = false;
Camera.main.GetComponent<BallFollow>().gameOver = true;
}
}
And this is my Camera Follow Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallFollow : MonoBehaviour
{
public GameObject ball;
Vector3 offset;
public float lerpRate;
public bool gameOver;
// Start is called before the first frame update
void Start()
{
offset = ball.transform.position - transform.position;
gameOver = false;
}
// Update is called once per frame
void Update()
{
if (!gameOver)
{
Follow();
}
}
void Follow()
{
Vector3 pos = transform.position;
Vector3 targetPos = ball.transform.position - offset;
pos = Vector3.Lerp(pos, targetPos, lerpRate * Time.deltaTime);
transform.position = pos;
}
}