Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by verschurengiovanni · Oct 06, 2020 at 01:28 AM · scripting problemscript.scripting beginner

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image verschurengiovanni · Oct 06, 2020 at 02:03 PM 0
Share

Thanks! But i'm still fairly new to C#. Where do I put the code you just described?

avatar image ryanjvigus verschurengiovanni · Oct 06, 2020 at 04:33 PM 1
Share

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!

avatar image verschurengiovanni ryanjvigus · Oct 09, 2020 at 12:06 AM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

266 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent won't work 1 Answer

Help me with this code proplem,,Almost finish my game please help this once 3 Answers

Instantiate an object on mouse/cursor position. 1 Answer

My Player is going through the floor after crouching 0 Answers

Play animation only when a key is being held down 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges