- Home /
How to increase the acceleration of the object and affect an emission materials brightness based on it
Hello all; I am creating a game like cubefield and tron legacy and I have came across a bump in the road. I dont know how to add acceleration to this movement script. Let alone correlate that with the emission output of a material. What I am trying to do is if the w key is pressed it will accelerate to a certain max speed and if it is let go it will slowly de-accelerate. I want the emission material to increase its brightness based on the objects speed and decrease also based on its speed. I just need help with those 2 things. Here is my movement script:
[Tooltip("How fast the character should move with no input, in world units per second")]
public float autoMoveSpeed = 40.0f;
[Tooltip("The direction the character should move with no input")]
public Vector3 autoMoveDirection = Vector3.right;
// Enforce that this is always a unit direction vector, so it doesn't bias our speed.
void OnValidate() { autoMoveDirection = autoMoveDirection.normalized; }
[Tooltip("How much the player can affect the speed in the direction of auto movement")]
public float parallelMoveSpeed = 50f;
[Tooltip("How fast the player can affect the speed perpendicular to the auto movement")]
public float perpendicularMoveSpeed = 70f;
[Tooltip("How many points to earn per second with no input")]
public float scorePerSecondAtDefaultSpeed = 10f;
[Tooltip("How many extra points to earn per second for every extra unit of speed")]
public float extraScorePerVelocityUnit = 1.0f;
// Track how much "loose change" points we've accumulated.
float _scoreAccumulator = 0f;
void Update()
{
// Capture the player's input vector.
Vector3 input = new Vector3(
Input.GetAxis("Horizontal"),
0,
Input.GetAxis("Vertical")
);
// Clamp the input to make sure we don't move faster on diagonals.
input = Vector3.ClampMagnitude(input, 1.0f);
// Extract the portion of the player's input parallel to the auto movement.
float parallelComponent = Vector3.Dot(autoMoveDirection, input);
// Leaving the component perpendicular to the auto movement.
Vector3 perpendicularComponent = (input - parallelComponent * autoMoveDirection);
// Compute how much to increase or decrease our auto movement based on our input.
float extraVelocity = parallelComponent * parallelMoveSpeed;
// Compute our net velocity, combining auto, parallel, and perpendicular movement.
Vector3 velocity = autoMoveDirection * (autoMoveSpeed + extraVelocity)
+ perpendicularComponent * perpendicularMoveSpeed;
// Translate us according to that velocity and the elapsed time.
transform.Translate(velocity * Time.deltaTime, Space.Self);
// Scoring:
// Compute our increased (or reduced) scoring rate based on our forward velocity.
float extraScore = extraVelocity * extraScorePerVelocityUnit;
// Increase our score accumulator based on our current earning rate and time passed.
_scoreAccumulator += (scorePerSecondAtDefaultSpeed + extraScore) * Time.deltaTime;
// Whenever we earn a whole point, send it to the score display as an integer.
int scoreUnits = Mathf.FloorToInt(_scoreAccumulator);
scorecript.scoreValue += scoreUnits;
// And decrement our accumulator to store just the "spare change".
_scoreAccumulator -= scoreUnits;
}
Your answer
Follow this Question
Related Questions
rigidbody acceleration 4 Answers
C# Move object by tilting on android device. 5 Answers
Multitouch: Second touch interferes with the first. 1 Answer
Car acceleration using Xbox 360 triggers? 1 Answer
acceleration setting 0 Answers