- Home /
How do I change 2d motor speed from a script?
Here is my code so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour {
private Rigidbody2D rb2d;
private HingeJoint2D jointRef;
private JointMotor2D motorRef;
public float speed = -100;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
jointRef = GetComponent<HingeJoint2D> ();
motorRef = jointRef.motor;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("left"))
motorRef.motorSpeed = speed;
}
void FixedUpdate ()
{
}
}
Looks like you fixed up your reference issue since last I saw this scipt. But it seems you still don;t understand motorSpeed. $$anonymous$$otorSpeed is a LI$$anonymous$$IT. Nothing you do it is going to make anything move. Did you find a tutorial yet?
I know it's a limit, but, if I'm correct, a motor will apply a constant force, set in the inspector, until it reaches that speed. What I'm trying to do, is change that max speed from 0 to "speed" when I want it to move.
Answer by unity_5F950F9CFD872127115E · Sep 11, 2021 at 11:15 AM
Create a new motor.
Set the values.
Assign the motor to the hingjoint.motor.
JointMotor2D motorRef = new JointMotor2D(); //Set values of motor jointRef.motor = motorRef;
This worked for me.
Your answer
Follow this Question
Related Questions
Cannot set motor speed in script 1 Answer
Cannot change 2d motor speed from script 3 Answers
2D Car problem with motor 0 Answers
trouble making a fast projectile 2 Answers