How to make Rotation of HingeJoint2D constant via mouse button C#
cant seem to complete a script that can initiate the constant rotation of a hinge joint for as long as the mouse button is pressed, and then free spin on the joint after depressing.
Answer by pRoFlT · Aug 24, 2015 at 04:46 AM
using UnityEngine;
using System.Collections;
public class junk_wheeloffourtune : MonoBehaviour {
public float velocity=1000;
public float force=20;
private HingeJoint joint;
private JointMotor motor1;
private bool buttonisdown=false;
void Start()
{
joint = GetComponent<HingeJoint>(); // Get hingejoint
joint.useMotor = false;
motor1.force = force;
motor1.targetVelocity = velocity;
joint.motor = motor1;
}
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
buttonisdown=true;
}
if(Input.GetMouseButtonUp(0))
{
buttonisdown=false;
}
if(buttonisdown)
{
joint.useMotor = true;
}
else
{
joint.useMotor = false;
}
}
}
okay probably not the best answer but it worked when i tried it.
i made a plane. added rigidbody, no gravity a Cylinder, added rigidbody, no gravity, angular drag 0.25 (pick one that feels right), a hingejoint on the Cylinder and connected it to the plane. picked y for my anchor and axis (not to sure which one was the right one so i changed them both)
force is how much force to put on the spin. velocity is what is the max velocity expected on the motor.
then i check if mouse down or if mouse up. if down turn on motor. if mouse up turn off motor.
Good Luck :)
Hi thinks for the reply, wll this work when converted to using joint2d ?
Sure. however 2D has different names on the motor controller. I cleaned it up a little and got rid of the button detect. mouse down and up are all you really need. See code:
using UnityEngine;
using System.Collections;
public class wheeloffourtune : $$anonymous$$onoBehaviour {
public float force=10;
public float speed=1000;
private HingeJoint2D joint;
private Joint$$anonymous$$otor2D motor1;
void Start()
{
joint = GetComponent<HingeJoint2D>(); // Get hingejoint
motor1.motorSpeed = speed;
motor1.max$$anonymous$$otorTorque = force;
joint.motor = motor1;
joint.use$$anonymous$$otor = false;
}
void Update ()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
joint.use$$anonymous$$otor = true;
}
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
joint.use$$anonymous$$otor = false;
}
}
}
for this i used 2 sprites. both with RigidBody2D. the second with HingeJoint2D and this script. connect first sprite as the anchor for the "connected Rigid Body" in the second sprite hinge component. Good luck :)
This worked perfectly, thankyou. is there a way to add a reverse option on mousebuttondown(1)) (right click) ?? just to get 2 way aim with the sprite
I think all you need is to change the force to a negative number.
Your answer
Follow this Question
Related Questions
OnMouseDrag() stopped working after few clicks 0 Answers
Detect mouse inputs when the scene is changing to another scene. 2 Answers
Camera Script remove a part. 0 Answers
Add rotative force in direction of mouse depending on how quick mouse is moved? 0 Answers
Left Mouse doesn't register click when keyboard button is pressed 1 Answer