- Home /
how to setup wheeljoint2D motor by code?
Hello all!
i'm trying to setup simple 2d car physics. i have a car with two WheelJoint2D component. each one of them hold one wheel. every thing is working fine beside the fact that i can't setup the motor by code (C#) here my simple script that should set the motor to -1000 and make the car jump with space key. (ignore that fact that it will jump once)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private bool isGrounded= true;
public float Power;
public float speed;
public WheelJoint2D wheelj1; // draged to this one the front wheel WheelJoint2D
public WheelJoint2D wheelj2; // draged to this one the back wheel WheelJoint2D
public JointMotor2D wheelj1jm;
public JointMotor2D wheelj2jm;
// Use this for initialization
void Start () {
speed = -1000f;
wheelj1jm = wheelj1.motor;
wheelj2jm = wheelj2.motor;
wheelj1.useMotor=true;
wheelj1jm.motorSpeed = speed; // try to setup the motor speed - nothing happen!
}
// Update is called once per frame
void FixedUpdate () {
if(Input.GetKey(KeyCode.Space) && isGrounded ){
this.rigidbody2D.AddForce(transform.up * Power);
isGrounded=false;
}
}
}
i tried to find some sample code for this but couldn't find any. i don't get any error but the motor don't get i value -1000. what am i doing wrong? thanks!
Answer by Hazlo · Jul 27, 2014 at 10:13 AM
Have you tried adding "wheelj1.motor = wheelj1jm;" after setting "wheelj1jm.motorSpeed = speed;" ?
not yet, but i will later today. it might work... thanks so far.
Your answer
Follow this Question
Related Questions
How do I change 2d motor speed from a script? 1 Answer
2D Car problem with motor 0 Answers
Character In Need Of Exorcism 1 Answer
Can you explain me please in more details what is motorTorque? 1 Answer
2D Physics, cannot pass -10 (y-axis) 1 Answer