Hello everyone…
I am making sample game like “no brakes”. Here is Video Link : NO BRAKES (iPhone Gameplay Video) - YouTube . 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,…
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.
– Ekta-Mehta-Dwhy you are not writing in FixedUpdate() any reason?
– bhartuOk, remove line with "else".
– zharik86