Drive Boat from start point to end point and vice versa
I am making a game same like river crossing iq game in unity. I have a boat. I need to float the boat from a point (call a start point) to end point. When Boat reached at the end point, Now I need to rotate the boat 180 degree and move to toward the start point and vice versa. I am newly in Unity. Please anyone help me.
Answer by Escrow_sanjay · Apr 05, 2017 at 12:42 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Boatmov : MonoBehaviour { public Rigidbody2D rb; float speed=1500f; float mspeed; public static bool distination=false; public static bool doublespeed=false; Vector2 forceapp; public Transform boatpos1, boatpos2; // Use this for initialization void Start () {
mspeed = speed * Time.deltaTime;
}
// Update is called once per frame
void Update () {
}
public void movment()
{
Debug.Log (distination);
if (distination == false) {
if (doublespeed == false) {
Vector2 mov = new Vector2 (boatpos2.position.x, boatpos2.position.y);
rb.AddForce (mov * mspeed);
rb.transform.localScale = new Vector3 (-2f, 1.5f, 0f);
doublespeed = true;
}
} else if(distination==true) {
if (doublespeed == false) {
Vector2 mov = new Vector2 (boatpos1.position.x,boatpos1.position.y);
rb.AddForce (mov * mspeed);
rb.transform.localScale = new Vector3 (2f, 1.5f, 0f);
doublespeed = true;
}
}
}
}
its a similar code over here you can use position inside of transform.position by inserting game object.