Search Unity

Help with controlling an object.

Discussion in 'Getting Started' started by BongMcPuffin, Oct 24, 2017.

  1. BongMcPuffin

    BongMcPuffin

    Joined:
    Oct 16, 2017
    Posts:
    26
    I am having trouble nailing down exactly the kind of movement I need out of my object.

    The goal is to have a tire that rolls across the ground in the direction you are looking, but it should only move along the ground even if I look into the sky and press forward, it shouldn't fly up into the sky. Here is a video of me rolling the tire around forwards (W), and then holding the backwards (S) while looking down to fly into the sky, which is behavior I do NOT want.




    What I would like is to disable going backwards at all and have S act more like the brakes on a car where it simply brings the tire to a stop instead of applying backwards force. If I look into the sky and hit forward, I don't want the tire to fly into the sky, I just want it to roll forward across the terrain and ignore the cameras vertical rotation. What I don't want is to clamp the tire to the ground though so it can still jump using the space bar to apply a force to the tire vertically so I can clear obstacles. I can figure out the jump code no problem (just haven't got that far yet), but I'm scratching my head on having the object ignore the vertical rotation of the camera while applying force to it and having it still move relative to where the camera is looking.

    I've gone through the Roll A Ball tutorials and this a slightly modified version of that code:

    Line 22 is the important bit that makes the object move in the direction of the camera.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TireControl : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(0.0f, 0.0f, moveVertical);
    22.         movement = Camera.main.transform.TransformDirection(movement);
    23.         rb.AddForce(movement * speed);
    24.     }
    25. }
    26.  
     
  2. all_iver

    all_iver

    Joined:
    Nov 11, 2013
    Posts:
    159
    You could try using Vector3.ProjectOnPlane(), something like:

    Code (CSharp):
    1. Vector3 projected = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up).normalized;
    2. Debug.DrawRay(transform.position, projected, Color.red);
    That should give you the camera's forward direction clamped to the ground.
     
  3. BongMcPuffin

    BongMcPuffin

    Joined:
    Oct 16, 2017
    Posts:
    26
    I found the IsGrounded function and it seems like it would be a perfect fit but it only works with character controllers, and when I added a character controller the tire wouldn't respond to input anymore so it seems to be cancelling out my rigidbody physics.