- Home /
Question by
SpaceMAXUE · Aug 21, 2018 at 11:48 AM ·
2drotationvariablespeed
Adding speed to the rotation every second
What I want to do is to make a script that will change the speed of a object every second, making it spin faster and faster (every second)
Here is the code that doesen't work :(
using UnityEngine;
using System.Collections;
public class moveFast : MonoBehaviour {
int p = 2;
int r = 1;
void Start() {
InvokeRepeating("Add", 1.0, 1.0);
function Add() {
p += r;
}
}
void Update()
{
transform.Rotate(0, 0, p);
}
}
}
How do I solve this problem, also I'm using 2D not 3D! The object is a ball, the error is :
Assets/moveFast.cs(10,21): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
Comment
Answer by gvergidis · Aug 21, 2018 at 12:32 PM
InvokeRepeating does not accept un named functions neither lambda. Try doing this :
private IEnumerator IncreaseSpeed()
{
p += r;
yield return new WaitForSeconds(1f);
StartCoroutine(IncreaseSpeed());
}
and add :
private void OnDisable()
{
this.StopAllCoroutines();
}
to make sure that this loop stops when this script is disabled.
Your answer
Follow this Question
Related Questions
rotation speed cap in 2D 0 Answers
Smoothed Rotation with 2D Top-Down View 1 Answer
turn fish3d follow mouse 0 Answers
Rotation used as movement 4 Answers
How do the change the distance of a 2D universal pipeline light from a script? 0 Answers