- Home /
Supposed movementspeed extremly low
Hello!
I made a movementscript, fairly basic for spaceships, as you can see I've capped it at 10 unity coordinates/second(?), something is extremly wrong thought as I can move at easily over 500 unity coordinates/ second. Whats wrong? Thank you in advance!
using UnityEngine;
using System.Collections;
public class Movementscript : MonoBehaviour {
Vector3 movementdirection;
Vector3 rotation;
public float pitchspeed;
public float yawspeed;
public float updownspeed;
public float acceleration;
public float maxspeed;
public float currentspeed;
// Use this for initialization
void Start () {
Vector3 rotation = new Vector3(0f,0f,0f);
}
// Update is called once per frame
void Update () {
//rotation = this.transform.localRotation;
rotation.x = Input.GetAxis("Horizontal") * -pitchspeed;
rotation.y = Input.GetAxis("Vertical") * -updownspeed;
rotation.z = Input.GetAxis("Yaw") * yawspeed;
movementdirection.x += Input.GetAxis("Acceleration") * acceleration * Time.fixedDeltaTime;
}
void FixedUpdate(){
if(movementdirection.x > maxspeed){
movementdirection.x = maxspeed;
}else if(movementdirection.x < -maxspeed){
movementdirection.x = -maxspeed;
}
currentspeed = movementdirection.x;
transform.Rotate(rotation.x, rotation.z, rotation.y, Space.Self);
transform.Translate(movementdirection, Space.Self);
}
}
Answer by Kiwasi · Jul 11, 2014 at 09:17 PM
Your not using Time.deltaTime. So maxspeed fixes the speed at units/FixedUpdate frame. FixedUpate runs at 50 fps by default.
10*50 = 500, which is your observed behaviour.
Fix by adding Time.deltaTime to your movement code. This will make sure the problem does not recur if you every change the physics time step.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
does not implement right interface 1 Answer
'The type or namespace name could not be found' 1 Answer
First child of a gameobject 3 Answers