Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tugberkoz · Apr 17, 2016 at 08:24 PM · gameobjectphysicscontrollerboat

Boat controller help

using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; using System.Collections;

public class boat1 : MonoBehaviour {

 public Graphic UI_Element;
 private Rigidbody rbody;
 public Slider rightSlider;
 RectTransform rectT;
 Vector2 centerPoint;
 public float maximumSteeringAngle = 180f;
 float wheelAngle = 0f;
 float wheelPrevAngle = 0f;
 bool wheelBeingHeld = false;
 
 float leftrightThrust;


 public float SubmitSliderSetting()
 {
     return rightSlider.value;
 }

 public float GetClampedValue()
 {
     
     return wheelAngle / maximumSteeringAngle;
 }
 public float GetAngle()
 {
     
     return wheelAngle;
 }

 void Start()
 {
     rectT = UI_Element.rectTransform;
     rbody = GetComponent<Rigidbody>();
     InitEventsSystem();
     UpdateRect();
     
 }

 void Update()
 {   leftrightThrust = GetClampedValue();
     
     Debug.Log((int)wheelAngle);
 }

 void FixedUpdate()
 {
     rbody.AddForce(transform.forward * Time.deltaTime * rightSlider.value, ForceMode.Acceleration);

     transform.Rotate(0, (int)wheelAngle, 0,Space.Self);

    
     rectT.localEulerAngles = Vector3.back * wheelAngle;
 }

 void InitEventsSystem()
 {

     EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();

     if (events == null)
         events = UI_Element.gameObject.AddComponent<EventTrigger>();

     if (events.triggers == null)
         events.triggers = new System.Collections.Generic.List<EventTrigger.Entry>();

     EventTrigger.Entry entry = new EventTrigger.Entry();
     EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();
     UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>(PressEvent);
     callback.AddListener(functionCall);
     entry.eventID = EventTriggerType.PointerDown;
     entry.callback = callback;

     events.triggers.Add(entry);

     entry = new EventTrigger.Entry();
     callback = new EventTrigger.TriggerEvent();
     functionCall = new UnityAction<BaseEventData>(DragEvent);
     callback.AddListener(functionCall);
     entry.eventID = EventTriggerType.Drag;
     entry.callback = callback;

     events.triggers.Add(entry);

     entry = new EventTrigger.Entry();
     callback = new EventTrigger.TriggerEvent();
     functionCall = new UnityAction<BaseEventData>(ReleaseEvent);//
     callback.AddListener(functionCall);
     entry.eventID = EventTriggerType.PointerUp;
     entry.callback = callback;

     events.triggers.Add(entry);
 }

 void UpdateRect()
 {
     
     Vector3[] corners = new Vector3[4];
     rectT.GetWorldCorners(corners);

     for (int i = 0; i < 4; i++)
     {
         corners[i] = RectTransformUtility.WorldToScreenPoint(null, corners[i]);
     }

     Vector3 bottomLeft = corners[0];
     Vector3 topRight = corners[2];
     float width = topRight.x - bottomLeft.x;
     float height = topRight.y - bottomLeft.y;

     Rect _rect = new Rect(bottomLeft.x, topRight.y, width, height);
     centerPoint = new Vector2(_rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f);
 }

 public void PressEvent(BaseEventData eventData)
 {
     
     Vector2 pointerPos = ((PointerEventData)eventData).position;

     wheelBeingHeld = true;
     wheelPrevAngle = Vector2.Angle(Vector2.up, pointerPos - centerPoint);
 }

 public void DragEvent(BaseEventData eventData)
 {
     
     Vector2 pointerPos = ((PointerEventData)eventData).position;

     float wheelNewAngle = Vector2.Angle(Vector2.up, pointerPos - centerPoint);

     
     if (Vector2.Distance(pointerPos, centerPoint) > 20f)
     {
         if (pointerPos.x > centerPoint.x)
             wheelAngle += wheelNewAngle - wheelPrevAngle;
         else
             wheelAngle -= wheelNewAngle - wheelPrevAngle;
     }

     
     wheelAngle = Mathf.Clamp(wheelAngle, -maximumSteeringAngle, maximumSteeringAngle);

     wheelPrevAngle = wheelNewAngle;
 }

 public void ReleaseEvent(BaseEventData eventData)
 {
     DragEvent(eventData);

     wheelBeingHeld = false;
 }

}

As you can see i am trying to make a boat controller and taking angle on the screen with this . alt text

there is 2 propeller on my boat. I want to rotate each of my propeller as wheel angle and push forward or backward effect like real propeller.Of course i am ignoring gravity, friction or too many rules in physics. I just want to change the rotation of my each propeller and make an forward effect on that direction. in 45 degrees of each propeller my ship's ahead goes right or in 90 degrees its effect more and turn right more than 45 degrees because my propeller in 90 degrees. Exactly i am traying to do ASD tugboat controller.

This script added on my rightengine which is a gameobject and it has a ridigbody and mesh collider also connected to the boat with hinge joint.

controller.png (27.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abdullahsyyd · Apr 26, 2016 at 06:55 AM 0
Share

$$anonymous$$y suggestion is that dont use transform.Rotate ins$$anonymous$$d use Rigidbody.$$anonymous$$oveRotation

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

any tutorial for playercontroll, physics ? 0 Answers

PlatformEffector2D on a game object with a RigidBody set to dynamic 0 Answers

Car steering on a Rigidbody (Mario Kart) 0 Answers

need help with using UFPS to control a ship 0 Answers

My game object won’t fall off of the cliff,My Enemies won’t fall off the cliff 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges