- Home /
Question by
IanMancuso · Jul 04, 2017 at 02:54 PM ·
2dscripting problemphysics2d-physicsmotor
Cannot set motor speed in script
I cannot seem to set the motor speed of a motor with this script. I can set it manually from within unity, but it does not work from the script. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour {
private Rigidbody2D rb2d;
private JointMotor2D motor;
public float speed = 100;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
motor = GetComponent<JointMotor2D> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("left"))
motor.motorSpeed = speed;
}
void FixedUpdate ()
{
}
}
Comment
Answer by efeguclu · Jul 04, 2017 at 03:12 PM
In this line :
public float speed = 100;
change it to :
public float speed = 100f;
forget this.
the problem is: there is no key called "left" try this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour {
private Rigidbody2D rb2d;
private JointMotor2D motor;
public float speed = 100f;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
motor = GetComponent<JointMotor2D> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space))
motor.motorSpeed = speed;
}
}
I think I got the problem . the problem is there is no key called "left" try this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : $$anonymous$$onoBehaviour {
private Rigidbody2D rb2d;
private Joint$$anonymous$$otor2D motor;
public float speed = 100f;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
motor = GetComponent<Joint$$anonymous$$otor2D> ();
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
motor.motorSpeed = speed;
}
}