- Home /
When game starts player is moving right on x axis by this small speed 0.5 , but how to make when game starts again to start to move player randomly on x axis(left or right)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class vvvv : MonoBehaviour {
public float speed = 11.4f;
private float move = 0.5f;
public Rigidbody2D rb;
private float movement = 0f;
static int score = 0;
static int hiScore = 0;
public Text scoreText;
public Text hiScoreText;
static public void AddPoint()
{
score++;
if (score > hiScore)
{
hiScore = score;
}
}
void Start()
{
score = 0;
hiScore = PlayerPrefs.GetInt("hiScore", 0);
}
void Update()
{
PlayerPrefs.SetInt("hiScore", hiScore);
scoreText.text = "Score:" + score;
hiScoreText.text = "HiScore:" + hiScore;
transform.Translate(move*Time.deltaTime , 0, 0);
// movement = Input.GetAxis("Horizontal") * speed;
// Move(Input.GetAxis("Horizontal"));
}
void FixedUpdate()
{
rb.MovePosition(rb.position + new Vector2(movement * Time.fixedDeltaTime, 0f));
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "Ball")
AddPoint();
}
public void Move(float moveInput)
{
movement = moveInput* speed;
}
}
Comment
I'd think you could so something like this.
private Vector3 direction;
void OnEnable()
{
if (Random.Range(0, 3) > 1)
{
direction = Vector3.right * -1;
}
else
direction = Vector3.right;
}
then just use the direction variable as your base movement direction.
Your answer
Follow this Question
Related Questions
Arcing and object along the X axis with a random amount 0 Answers
Random movement, staying in an area. 1 Answer
Random Movement of Enemy within a defined area 2 Answers
how can i move a platform/base in a x-z plane automatically within a given range 1 Answer
How to make brownian motion of a RigidBody animal AI? 1 Answer