- Home /
Rigidbody2D Force Problem
Hello everyone..
I am making sample game like "no brakes". Here is Video Link : http://www.youtube.com/watch?v=Lb2sQJfqZnE . I have done coding for moving player.
Code :
void Update () {
if(ON)
{
rigidbody2D.drag = 1.2f;
rigidbody2D.angularDrag = 0.05f;
speedTxt.text = velocity.ToString ();
SpeedControl();
Movement();
}
}
void Movement()
{
rigidbody2D.AddForce (transform.up * speed );
velocity = Mathf.Round (rigidbody2D.velocity.magnitude * 10.6f);
if(Input.GetMouseButton(0))
{
Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
inputPos.z = 0;
RaycastHit2D hit;
hit = Physics2D.Raycast( inputPos,new Vector2( 0,0 ) );
if(hit.collider.name == "Left")
{
Debug.Log("left.. ");
yRotation += 3.0f ;
Quaternion target = Quaternion.Euler(0, 0, yRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 10.0f);
rigidbody2D.AddForce(-transform.right * speed * 1.1f );
//rigidbody2D.AddForce (transform.up * 1.2f );
rigidbody2D.drag += 0.1f;
//rigidbody2D.angularDrag += 0.5f;
}
if(hit.collider.name == "Right")
{
Debug.Log("Right.. ");
yRotation -= 3.0f ;
Quaternion target = Quaternion.Euler(0, 0, yRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 10.0f);
rigidbody2D.AddForce(transform.right * speed * 1.1f );
//rigidbody2D.AddForce (transform.up * 1.2f );
rigidbody2D.drag += 0.1f;
//rigidbody2D.angularDrag += 0.5f;
}
}
}
All this i have did in 2d scene.
In this code issue is while moving left and right.. its not going smoothly like it should go forward also and should turn also. I want exact behavior like in this video.
I am trying from last two days but i am not able to solve this.
Please anyone help me.
Thanks,..
[1]: http://www.youtube.com/watch?v=Lb2sQJfqZnE
Answer by zharik86 · Oct 08, 2014 at 06:57 AM
First, your angle, yRotation is equal to 3 degrees. It is difficult to see such deviation on a scene. Reject your object of degrees on 20. Also set the speed of change of a deviation, for example, 5. The second, in your script I don't see resetting to an original status of rotation in case of release of the button. I will a little rewrite your script as I represent it:
public float speedRot = 5.0f; //add one more variable for speed of rotation. For change value, change it in Inspector.
void Start() {
//Maybe, initialization your rigidbody here
}
void Update () {
if(ON) {
rigidbody2D.drag = 1.2f;
rigidbody2D.angularDrag = 0.05f;
speedTxt.text = velocity.ToString ();
SpeedControl();
Movement();
}
}
void Movement() {
rigidbody2D.AddForce (transform.up * speed );
velocity = Mathf.Round (rigidbody2D.velocity.magnitude * 10.6f);
if(Input.GetMouseButton(0)) {
Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
inputPos.z = 0;
RaycastHit2D hit;
hit = Physics2D.Raycast( inputPos,new Vector2( 0,0 ) );
if(hit.collider.name == "Left") {
Debug.Log("left.. ");
yRotation += 20.0f; //change angle to 20 degree
Quaternion target = Quaternion.Euler(0, 0, yRotation);
//Apply our variable speedRot
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * speedRot);
rigidbody2D.AddForce(-transform.right * speed * 1.1f );
//rigidbody2D.AddForce (transform.up * 1.2f );
rigidbody2D.drag += 0.1f; //Why change, you reinitialization your rigidbody in Update every frame. Isn't necessary.
//rigidbody2D.angularDrag += 0.5f;
}
if(hit.collider.name == "Right") {
Debug.Log("Right.. ");
yRotation -= 20.0f;
Quaternion target = Quaternion.Euler(0, 0, yRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * speedRot);
rigidbody2D.AddForce(transform.right * speed * 1.1f );
//rigidbody2D.AddForce (transform.up * 1.2f );
rigidbody2D.drag += 0.1f;
//rigidbody2D.angularDrag += 0.5f;
}
}
}
I hope that it will help you.
No in this game its not resetting the rotation. It will move towards that direction. U can play that game. But while moving its stopes moving forward.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Make Bumper Effect 0 Answers
Adding Foward Force to A Rigidbody2D 0 Answers
Rigidbody2D won't stop moving. 2 Answers