- Home /
Speed up with Rigidbody as frame rate increases c#
I am using a Rigidbody controller and it has a habit of speeding up when the framerates increase. I attempted to fix this by using * time.deltatime, and that did make the increase in speed not as extreme but still there.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody))]
public class Movment : MonoBehaviour
{
//.ToString("F1")
[Header("Speed Setting")]
public float speed = 10.0f;
public float gravity = 10.0f;
public float maxVelocityChange = 10.0f;
public float Torquespeed = 0f;
public float maxTorqueChange = 0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
[Header("Information Output")]
public Vector3 velocity;
public Vector3 torque;
public Text Torque;
public Text Speed;
public Text Location;
private bool grounded = false;
Rigidbody rigidbody;
Vector3 targetVelocity = new Vector3();
Vector3 targetTorque = new Vector3();
void Awake()
{
rigidbody = this.GetComponent<Rigidbody>();
rigidbody.freezeRotation = false;
rigidbody.useGravity = false;
}
void FixedUpdate()
{
velocity = rigidbody.velocity;
// Calculate how fast we should be moving
//Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")
if (Input.GetAxis("Horizontal") > 0 && velocity.x < maxVelocityChange)
{ targetVelocity.x += Input.GetAxis("Horizontal");
if (velocity.x > maxVelocityChange * Time.deltaTime) { targetVelocity.x = maxVelocityChange * Time.deltaTime; }
}
if (Input.GetAxis("Horizontal") < 0 && velocity.x > -maxVelocityChange)
{ targetVelocity.x += Input.GetAxis("Horizontal");
if (velocity.x < -maxVelocityChange * Time.deltaTime) { targetVelocity.x = -maxVelocityChange * Time.deltaTime; }
}
if (Input.GetAxis("Vertical") > 0 && velocity.z < maxVelocityChange)
{ targetVelocity.z += Input.GetAxis("Vertical");
if (velocity.z > maxVelocityChange * Time.deltaTime) { targetVelocity.z = maxVelocityChange * Time.deltaTime; }
}
if (Input.GetAxis("Vertical") < 0 && velocity.z > -maxVelocityChange)
{ targetVelocity.z += Input.GetAxis("Vertical");
if (velocity.z < -maxVelocityChange * Time.deltaTime) { targetVelocity.z = -maxVelocityChange * Time.deltaTime; }
}
targetVelocity.y = 0;
//targetTorque
if (Input.GetAxis("Rotate Vertical") > 0 && Input.GetAxis("Rotate Vertical") > targetTorque.x)
{ targetTorque.x = Input.GetAxis("Rotate Vertical"); }
if (Input.GetAxis("Rotate Vertical") < 0 && Input.GetAxis("Rotate Vertical") < targetTorque.x)
{ targetTorque.x = Input.GetAxis("Rotate Vertical"); }
if (Input.GetAxis("Rotate Horizontal") > 0 && Input.GetAxis("Rotate Horizontal") > targetTorque.y)
{ targetTorque.y = Input.GetAxis("Rotate Horizontal"); }
if (Input.GetAxis("Rotate Horizontal") < 0 && Input.GetAxis("Rotate Horizontal") < targetTorque.y)
{ targetTorque.y = Input.GetAxis("Rotate Horizontal"); }
//Stop1
if (Input.GetAxis("Jump") >0.5) {
if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
targetVelocity.y = 0;
if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
targetTorque.z = 0;
}
if (Input.GetAxis("Stop1") > 0.5)
{
if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
targetTorque.z = 0;
}
if (Input.GetAxis("Stop2") > 0.5)
{
if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
targetVelocity.y = 0;
}
// Apply a force that attempts to reach our target velocity
Vector3 ProsTargetVelocity = transform.TransformDirection(targetVelocity);
ProsTargetVelocity *= speed;
Vector3 velocityChange = (ProsTargetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
velocityChange *= Time.deltaTime;
torque = rigidbody.angularVelocity;
Vector3 ProsTargetTorque = transform.TransformDirection(targetTorque);
ProsTargetTorque *= Torquespeed;
Vector3 TorqueChange = (ProsTargetTorque - torque);
TorqueChange.x = Mathf.Clamp(TorqueChange.x, -maxTorqueChange, maxTorqueChange);
TorqueChange.y = Mathf.Clamp(TorqueChange.y, -maxTorqueChange, maxTorqueChange);
TorqueChange.z = 0;
TorqueChange *= Time.deltaTime;
Vector3 speedTrack = transform.InverseTransformDirection(velocity);
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
rigidbody.AddTorque(TorqueChange, ForceMode.VelocityChange);
Speed.text = "Speed: " + speedTrack.x.ToString("F1") + "," + speedTrack.y.ToString("F1") + "," + speedTrack.z.ToString("F1");
Location.text = "Location: " + transform.position.x.ToString("F1") + "," + transform.position.y.ToString("F1") + "," + transform.position.z.ToString("F1");
//Torque.text = "Torque: " + TorqueChange.x.ToString("F1") + "," + TorqueChange.y.ToString("F1") + "," + TorqueChange.z.ToString("F1");
// We apply gravity manually for more tuning control
//rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay()
{
grounded = true;
}
}
Answer by Hez · Jan 08, 2017 at 03:56 AM
I had to completely rewrite the script. I am still not exactly sure what it was that was wrong, but I got it to work without speeding up upon framerate change. I feel kind of nubeish about a movement script keeping me down for a few days.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody))]
public class Movment : MonoBehaviour
{
//.ToString("F1")
[Header("Speed Setting")]
public float speed = 10.0f;
public float gravity = 10.0f;
public float maxVelocityChange = 10.0f;
public float Torquespeed = 0f;
public float maxTorqueChange = 0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
[Header("Information Output")]
public Vector3 velocity;
public Vector3 torque;
public Text Torque;
public Text Speed;
public Text Location;
private bool grounded = false;
Rigidbody rigidbody;
Vector3 targetVelocity = new Vector3();
Vector3 targetTorque = new Vector3();
void Awake()
{
rigidbody = this.GetComponent<Rigidbody>();
rigidbody.freezeRotation = false;
rigidbody.useGravity = false;
}
void FixedUpdate()
{
velocity = rigidbody.velocity;
// Calculate how fast we should be moving
//Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")
if (Input.GetAxis("Horizontal") != 0)
{ targetVelocity.x += Input.GetAxis("Horizontal");
}
if (Input.GetAxis("Vertical") != 0)
{ targetVelocity.z += Input.GetAxis("Vertical");
}
targetVelocity.y = 0;
//targetTorque
if (Input.GetAxis("Rotate Vertical") > 0 && Input.GetAxis("Rotate Vertical") > targetTorque.x)
{ targetTorque.x = speed; }
if (Input.GetAxis("Rotate Vertical") < 0 && Input.GetAxis("Rotate Vertical") < targetTorque.x)
{ targetTorque.x = -speed; }
if (Input.GetAxis("Rotate Horizontal") > 0 && Input.GetAxis("Rotate Horizontal") > targetTorque.y)
{ targetTorque.y = speed; }
if (Input.GetAxis("Rotate Horizontal") < 0 && Input.GetAxis("Rotate Horizontal") < targetTorque.y)
{ targetTorque.y = -speed; }
//Stop1
if (Input.GetAxis("Jump") >0.5) {
if (targetVelocity.x > 0) { targetVelocity.x -= speed / 4; if (targetVelocity.x < 0) targetVelocity.x = 0; }
if (targetVelocity.x < 0) { targetVelocity.x += speed / 4; if (targetVelocity.x > 0) targetVelocity.x = 0; }
if (targetVelocity.z > 0) { targetVelocity.z -= speed / 4; if (targetVelocity.z < 0) targetVelocity.z = 0; }
if (targetVelocity.z < 0) { targetVelocity.z += speed / 4; if (targetVelocity.z > 0) targetVelocity.z = 0; }
targetVelocity.y = 0;
if (targetTorque.x > 0) { targetTorque.x -= Torquespeed / 4; if (targetTorque.x < 0) targetTorque.x = 0; }
if (targetTorque.x < 0) { targetTorque.x += Torquespeed / 4; if (targetTorque.x > 0) targetTorque.x = 0; }
if (targetTorque.y > 0) { targetTorque.y -= Torquespeed / 4; if (targetTorque.y < 0) targetTorque.y = 0; }
if (targetTorque.y < 0) { targetTorque.y += Torquespeed / 4; if (targetTorque.y > 0) targetTorque.y = 0; }
targetTorque.z = 0;
//targetTorque.y = 0;
//targetTorque.x = 0;
}
if (Input.GetAxis("Stop1") > 0.5)
{
if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
targetTorque.z = 0;
}
if (Input.GetAxis("Stop2") > 0.5)
{
if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
targetVelocity.y = 0;
}
if (targetVelocity.x > maxVelocityChange) { targetVelocity.x = maxVelocityChange; }
if (targetVelocity.x < -maxVelocityChange) { targetVelocity.x = -maxVelocityChange; }
if (targetVelocity.y > maxVelocityChange) { targetVelocity.y = maxVelocityChange; }
if (targetVelocity.y < -maxVelocityChange) { targetVelocity.y = -maxVelocityChange; }
if (targetVelocity.z > maxVelocityChange) { targetVelocity.z = maxVelocityChange; }
if (targetVelocity.z < -maxVelocityChange) { targetVelocity.z = -maxVelocityChange; }
if (targetTorque.x > maxTorqueChange) { targetTorque.x = maxTorqueChange; }
if (targetTorque.x < -maxTorqueChange) { targetTorque.x = -maxTorqueChange; }
if (targetTorque.y > maxTorqueChange) { targetTorque.y = maxTorqueChange; }
if (targetTorque.y < -maxTorqueChange) { targetTorque.y = -maxTorqueChange; }
if (targetTorque.z > maxTorqueChange) { targetTorque.z = maxTorqueChange; }
if (targetTorque.z < -maxTorqueChange) { targetTorque.z = -maxTorqueChange; }
torque = rigidbody.angularVelocity;
Vector3 ProsTargetTorque = transform.TransformDirection(targetTorque);
ProsTargetTorque *= Torquespeed;
Vector3 TorqueChange = (ProsTargetTorque - torque);
TorqueChange.x = Mathf.Clamp(TorqueChange.x, -maxTorqueChange, maxTorqueChange);
TorqueChange.y = Mathf.Clamp(TorqueChange.y, -maxTorqueChange, maxTorqueChange);
TorqueChange.z = 0;
rigidbody.velocity = transform.TransformDirection(targetVelocity);
rigidbody.AddRelativeTorque(TorqueChange, ForceMode.VelocityChange);
Vector3 speedTrack = transform.InverseTransformDirection(velocity);
Speed.text = "Speed: " + speedTrack.x.ToString("F1") + "," + speedTrack.y.ToString("F1") + "," + speedTrack.z.ToString("F1");
Location.text = "Location: " + transform.position.x.ToString("F1") + "," + transform.position.y.ToString("F1") + "," + transform.position.z.ToString("F1");
//Torque.text = "Torque: " + TorqueChange.x.ToString("F1") + "," + TorqueChange.y.ToString("F1") + "," + TorqueChange.z.ToString("F1");
grounded = false;
}
void OnCollisionStay()
{
grounded = true;
}
}
Your answer
Follow this Question
Related Questions
Moving a CharacterController Object together with a RigidBody Object 1 Answer
Distribute terrain in zones 3 Answers
Convert Rigidbody code to CharacterController usable 0 Answers
Collision on character controller (fps player) 0 Answers
Having problems with animation and character controller velocity not matching 0 Answers