Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Some issues with the idea of creating a vehicle that is based off of player interaction.

Discussion in 'Physics' started by Geeknerd1337, Jan 11, 2015.

  1. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    52
    Okay, so I have the first person controller and the code set up and ready for a sort of hovering vehicle.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HoverMotor : MonoBehaviour {
    5.    
    6.     public float speed = 90f;
    7.     public float turnspeed = 5f;
    8.     public float hoverforce = 65f;
    9.     public float hoverheight = 1f;
    10.    
    11.     public bool canride = false;
    12.    
    13.     private float powerInput;
    14.     private float turnInput;
    15.     private Rigidbody carigidbody;
    16.    
    17.     // Use this for initialization
    18.     void Awake () {
    19.    
    20.         carigidbody = GetComponent <Rigidbody>();
    21.        
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.        
    27.         powerInput = Input.GetAxis ("Vertical");
    28.         turnInput = Input.GetAxis ("Horizontal");
    29.    
    30.     }
    31.    
    32.     void FixedUpdate()
    33.     {
    34.         Ray ray = new Ray (transform.position, -transform.up);
    35.         RaycastHit hit;
    36.        
    37.         if (Physics.Raycast(ray, out hit, hoverheight))
    38.         {
    39.             float proportionalheight = (hoverheight - hit.distance) / hoverheight;
    40.             Vector3 appliedhoverforce = Vector3.up * proportionalheight * hoverforce;
    41.             carigidbody.AddForce(appliedhoverforce, ForceMode.Acceleration);
    42.                
    43.            
    44.         }
    45.        
    46.         carigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
    47.        
    48.         carigidbody.AddRelativeTorque(0f, turnInput * turnspeed, 0f);
    49.    
    50.     }
    51. }
    52.  
    Fairly simple stuff. However, my player obviously slips and slides around on it as I try and drive it. So I have a few questions.

    One, how do I set up a trigger and/or button that will deactivate whether or not my controls are moving this thing unless the player is on it or has clicked something that would signify the wheel.

    Had do I lock and unlock a player in position when they click on said wheel or enter said craft.

    Please, any responses to this conundrum will be heavily appreciated. I think that this could end up having some nice effects when it's done.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    A collider has a property called isTrigger that sets it up so that it doesn't actually collide with incoming objects but merely detects that they've made contact. You could place an invisible trigger collider in the vehicle and add code in an OnTriggerEnter function to detect when the player has entered the vehicle.

    As for keeping the player stationary inside the vehicle, you could set its isKinematic property to true (to temporarily disable its physics) and then just parent it to the vehicle object. When you detect that the player wants to leave the vehicle then unparent it again and set isKinematic back to false.