Hi! I’ve problem with set the acceleration in my script.
I created a maxSpeed var, so that the speed should reach the max speed gradually. But ther’s something wrong
PS: i’m a beginner with scripting.
PS I would like that If i press A or S everytime the acceleration is gradually, then it return to its value
Thank you : )
This work, but on time that maxspeed has reach 10, it doesn’t graduate the speed anymore.
I would like that only when I press A or S the speed start gradually, but always with the same speed. Only the start should be graduated. Hope I have explained
So, this script moves an object up and down, ok, but the movement when I press A or S is is jerky. The speed is correct so, but when the object start i don’t like the jerky movement, but i would like a “soft” starting everytime I press the input keys. (A, S)…
I am just going to show you my solution, then work you through the steps. It took me an embarrasing amount of time before I finally understood acceleration and velocity, so I know where you’ re coming from!
Anyway, this ought to do it:
using UnityEngine;
using System.Collections;
public class CubeController : MonoBehaviour {
public float maxSpeed = 1f;
public float acceleration = 0.2f;
public float currentSpeed = 0f;
void Start () {
acceleration *= Time.deltaTime;
}
void Update() {
if (Input.GetKey(KeyCode.A)) {
currentSpeed += acceleration;
transform.Translate(transform.up * currentSpeed, Space.World);
} else if (Input.GetKey(KeyCode.S)) {
currentSpeed += acceleration;
transform.Translate(-transform.up * currentSpeed, Space.World);
} else {
currentSpeed = 0;
//or, if you wish to de-accelerate:
//currentSpeed -= acceleration;
}
currentSpeed = Mathf.Clamp(currentSpeed,0f,maxSpeed);
}
}
I just applied this CS script to a simple cube in the scene (GameObject → Create Other → Cube)
Acceleration, as @SubZeroGaming_1 pointed out, is the change in speed over time. Make sure you understand this before you go any further. If you sit in a car and hit the gas pedal, first the car moves slowly, then it accelerates (it moves faster, then faster still, then even faster).
Alright, let’s step through the code:
public float maxSpeed = 1f;
public float acceleration = 0.2f;
public float currentSpeed = 0f;
^ We’re just setting up our variables. We have a maximum speed of 1f (Unity world points, not pixels!), and with acceleration we say that we want to accelerate with 0.2f points every second. The important bit is every second. Keep this in mind. Lastly, we just have the current speed which is the speed the cube is currently travelling at (up initialization, it is 0).
//calculates how many units we have to accelerate every Update() so that our acceleration over 1 second is 0.2
void Start () {
acceleration *= Time.deltaTime;
}
^ This is a very important line of code. Remember how I said I wanted to move 0.2f every second?
You’ d say we could just add 0.2f to our speed in the Update() function, right?
Wrong!
The Update() function is called far more often than every second. Time.deltaTime tells you how much time has passed since the last call to Update(). By multiplying our desired acceleration per second (0.2) by the deltaTime we get how many units we have to move this update to have moved a total of 0.2 in a second. Remember, Update() gets called very often and so we just need to translate a tiny bit every update to get an overall smooth movement in a second.
Alright, our currenSpeed grows (accelerates) by the acceleration. We were travelling at currentSpeed 0 at first, but now that has been increased to 0 + acceleration.
Next, we translate our cube along the Y-axis. I noticed you used a vector here which is perfectly fine, but I get the feeling you may or may not really understand what you’re doing here. In any case, it could also be written like this:
Lastly, that ^ simply makes sure that our currentSpeed never becomes less than 0 and never more than our maximum speed.
Completely off topic, hi @SubZeroGaming_1 ! I am checking out your channel. I am new to Unity, but I’ve done my fair share of C# and Java. Your tutorials are quite helpful!
Okay I haven’ t tried this in Unity, but I am pretty sure the reason nothing is moving is because you don’t try to move anything.
In game design terms, movement = translation. Where is your call to transform.translate();? All I see is transform.rotate.
In this case you forgot to code transform.Translate(); you need to add in into your existing script. It must go after the rotation code. There is a specific order of operations. It goes:
EDIT: Again, I haven’t tried your code yet, so I am not 100% sure how it works, but those while loops within the if statements look suspicious to me. Do you really need them?
No it doesn’t work. Now I show you the code without add the things that you said me before.
So the code work, before, I tried to add your part of code and it doesn’t work anymore.
I can’t edit this code because so is the only way to do what i would like.
using UnityEngine;
using System.Collections;
public class Braccio : MonoBehaviour {
public GameObject[] arm;
public float speed;
Vector3 StartPosition;
// Update is called once per frame
void Update () {
//go up
if (Input.GetKey (KeyCode.Z)) {
float currentAngle = transform.rotation.eulerAngles.x;
while (currentAngle < -180.0f)
currentAngle += 360.0f;
while (currentAngle > 180.0f)
currentAngle -= 360.0f;
Debug.Log ("currentAngle " + currentAngle);
float destinationAngle = currentAngle - Time.deltaTime * speed;
Debug.Log ("destinationAngle before clamp " + destinationAngle);
destinationAngle = Mathf.Clamp (destinationAngle, -50.0f, 0.0f);
Debug.Log ("destinationAngle after clamp " + destinationAngle);
float deltaAngle = destinationAngle - currentAngle;
transform.Rotate(Vector3.right, deltaAngle);
}
//go down
if (Input.GetKey (KeyCode.X)) {
float currentAngle = transform.rotation.eulerAngles.x;
while (currentAngle < -180.0f)
currentAngle += 360.0f;
while (currentAngle > 180.0f)
currentAngle -= 360.0f;
Debug.Log ("currentAngle " + currentAngle);
float destinationAngle = currentAngle + Time.deltaTime * speed;
Debug.Log ("destinationAngle before clamp " + destinationAngle);
destinationAngle = Mathf.Clamp (destinationAngle, -50.0f, 0.0f);
Debug.Log ("destinationAngle after clamp " + destinationAngle);
float deltaAngle = destinationAngle - currentAngle;
transform.Rotate(Vector3.right, deltaAngle);
}
}
}
I am trying to accomplish something with acceleration, but over time without me pressing anything, lets say there is an enemy with a base speed of 2, but every time I (the player) reached certain amount of points in score, the enemy gain 0.50f of speed, then after another amount of points is reached then the another amount of speed is added to the enemy. Is there any way to make this possible?