- Home /
Slowly increase motor.force
Hello!
I created a fairly simple script to add motor.force/velocity to a gameobject. But, I want it to increase it over time and not instant.
But i'm not sure on how to achieve this. Still fairly new to C#.
If anyone can help me with this, that will be highly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArmMotor : MonoBehaviour
{
private HingeJoint _hingeJoint;
// Start is called before the first frame update
void Start()
{
_hingeJoint = GetComponent<HingeJoint>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("e"))
{
JointMotor motor = _hingeJoint.motor;
motor.force = 0;
motor.targetVelocity = 0;
_hingeJoint.motor = motor;
}
if (Input.GetKeyDown("d"))
{
JointMotor motor = _hingeJoint.motor;
motor.force = 1500;
motor.targetVelocity = 90000;
_hingeJoint.motor = motor;
}
}
}
Answer by ryanjvigus · Oct 06, 2020 at 07:00 AM
The easiest way to do this is probably just to add an amount multiplied by Time.deltaTime (which is the time between frames) when the button is held and subtracting in the same way when 'e' is held, and just checking to see if the value is greater or less than the desired value.
speed = 1;
if (motor.force <= 1500)
motor.force += speed * Time.deltaTime;
else
motor.force = 1500;
The else at the end is just to make sure it doesn't go over the destination value. Also just in case you're unfamiliar, writing 'motor. force += x' is the same as writing 'motor.force = motor.force + x' You could also potentially use the function Mathf.Lerp (), but I think this way is the easiest method for what you want to do.
Thanks! But i'm still fairly new to C#. Where do I put the code you just described?
Sure! So actually I've never dealt with hinge joints or motors before and they seem kinda complicated but I've done my best to complete your code to make it do what you want it to. Let me know if it works or not and try your best to try and understand what I've done and ask if you don't get anything so that you know how to do stuff like that in the future :)
public class Arm$$anonymous$$otor : $$anonymous$$onoBehaviour
{
//I'm not really sure how force and targetVelocity affect the HinjeJoint so I just made separate
//speed values for force and velocity that you can change however you want
public float forceSpeed;
public float velocitySpeed;
//I prefer simpler names like this but feel free to change it back of course
private HingeJoint joint;
void Start ()
{
joint = GetComponent<HingeJoint> ();
}
void Update ()
{
Joint$$anonymous$$otor motor = joint.motor;
//I changed GetKeyDown to GetKey because GetKeyDown is only true on the first frame
//the button is pressed and we want to change the value for every frame it's held
if (Input.GetKey ("e"))
{
//This is a better way of doing it than before, $$anonymous$$athf.Clamp makes sure that any value you give it
//Stays between a range (in this case 0 and 1500) so we can just add or subtract without worrying about it
motor.force = $$anonymous$$athf.Clamp (motor.force - forceSpeed * Time.deltaTime, 0, 1500);
motor.targetVelocity = $$anonymous$$athf.Clamp (motor.targetVelocity - velocitySpeed * Time.deltaTime, 0, 90000);
}
if (Input.GetKey ("d"))
{
motor.force = $$anonymous$$athf.Clamp (motor.force + forceSpeed * Time.deltaTime, 0, 1500);
motor.targetVelocity = $$anonymous$$athf.Clamp (motor.targetVelocity + velocitySpeed * Time.deltaTime, 0, 90000);
}
joint.motor = motor;
}
}
hopefully it helps!
Thanks! That totally helped!
And what if I wanted to use the spring and the spring demper?
What do I need to change in order for it to work? :)
Sorry for all those questions, still learning!