- Home /
How to make the ball rotate
I have an endless runner game that the ball keeps moving forward and the player can move it only right and left but the problem is the ball move forward without rotating I tried "transform.Rotate(r,0,0);" when "r" is variable that keeps rotating and accelerates but the object rotates around another point not its axis. (the function move() is the one responsible for moving it forward) of corse I don't use gravity
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class mover : MonoBehaviour { [SerializeField] float moveSpeed = 10f; [SerializeField] float delay = 5f; [SerializeField] float movingSpeed= 1f; [SerializeField] float speedAcceleration = 0.0001f; private Touch touch; private float touchSpeed = 0.02f; public static bool gameStarted = false;
public static bool dead = false;
[SerializeField] public static float timer = 0.0f;
public GameObject diePanel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
timerCount();
if ( SwipeManager.swipeLeft||SwipeManager.swipeRight ||SwipeManager.swipeUp ||SwipeManager.swipeDown|| Input.GetKeyDown("w"))
{
gameStarted = true;
}
move();
Movetouch();
}
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "obestcle")
{
gameStarted = false;
dead = true;
diePanelopen();
//Invoke("tryagain",delay);
}
}
void move(){
if (dead == false && gameStarted == true )
{
movingSpeed = movingSpeed + speedAcceleration;
float x = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed ;
float z = Time.deltaTime * moveSpeed * movingSpeed ;
transform.Translate(x,0,z);
}
}
void Movetouch()
{
if (Input.touchCount > 0 )
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved && dead == false && gameStarted == true)
{
if (transform.position.x + touch.deltaPosition.x * touchSpeed > 9)
{
transform.position = new Vector3( 9, transform.position.y,transform.position.z);
}
else if (transform.position.x + touch.deltaPosition.x * touchSpeed < -8 )
{
transform.position = new Vector3(-8, transform.position.y,transform.position.z);
}
else
{
transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * touchSpeed , transform.position.y,transform.position.z);
}
}
}
}
void tryagain()
{
dead = true;
gameStarted = false;
diePanelopen();
}
void timerCount()
{
if (gameStarted == true)
{
timer += Time.deltaTime;
}
}
void diePanelopen()
{
if(diePanel != null)
{
bool isActive = diePanel.activeSelf;
diePanel.SetActive(!isActive);
mover.gameStarted = false;
}
else if (Restart.isPanelOffOnRestart == true)
{
diePanel.SetActive(false);
Restart.isPanelOffOnRestart = false;
}
}
}
didn't work maybe I didn't write it in a proper way but its not working
Your answer
![](https://koobas.hobune.stream/wayback/20220613033947im_/https://answers.unity.com/themes/thub/images/avi.jpg)