Hi,
we are developing a multiplayer game based on vehicles that move only forward but when they are near a column they can rotate around it and then exit from the circular movement with a linear and tangent one.
The mechanic is really similar to One More Line. Here a graphical scheme to represent the mechanic (in green: collider that detects if the button is pressed)
We don’t know how to make entry end exit tangent to the collider. We found the function LookAt but we don’t know how to use it to make only one axis to point the column, not the entire vehicle’s model. In the prototype, we started with a vehicle that is going towards the column as showed in the previous sketch, so for the moment, as you can see in the code, I tried to rotate of 90° (Y axis) the vehicle but doing so it happens that the hook collider and the vehicle’s collider doesn’t touch anymore, so I was trying to find a solution to make the transition from linear direction to tangent in a smooth way.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
private Rigidbody rb;
public GameObject hook; //sensor that detects if the vehicle is eligible to rotate
public bool rotationStarted; //to detect if the vehicle is rotating or not
public int speed; //speed of vehicle when in linear motion
void Start()
{
rb = GetComponent<Rigidbody>();
rb.AddForce(transform.forward * speed); //adding initial force to Z axis
rotationStarted = false; //the vehicle does not rotate at the beginning
}
void OnTriggerStay(Collider other)
{
//detect if the vehicle is in the action range of the sensor "hook" and if the player is keeping pressed the button
if (other.gameObject.CompareTag("hook") && (Input.GetKey(KeyCode.UpArrow)))
{
//column = GameObject.FindWithTag("hook"); WARNING: this function doesn't work as expected, because it identifies as object the last one to have the tag "hook", not the true touched object
//detect if the vehicle is passing from linear movement to rotation
if (rotationStarted == false)
{
rotationStarted = true;
//WARNING: the following 4 lines are trying to lerp the rotation but it's infinite, so we must find a way to terminate the rotation
var fromAngle = transform.eulerAngles;
var targetAngle = transform.eulerAngles + new Vector3(0, -90, 0);
transform.eulerAngles = Vector3.Lerp(fromAngle, targetAngle, 0.01f);
Debug.Log(transform.eulerAngles + " " + fromAngle + " " + targetAngle);
// transform.Rotate(0, -90, 0); //NOT EFFICIENT: if you press the button when it starts to touch the hook, vehicle's collider doesn't touch the hook anymore; I think we should use Lerp
//transform.LookAt(column.transform); TEMPORALY DISABLED: we must find a way to LookAt only with an axis, not with the entire vehicle's model
}
rb.velocity = Vector3.zero; //stopping the linear movement
transform.RotateAround(hook.transform.position, Vector3.up, 100 * Time.deltaTime); //rotating around the column
Debug.Log("Rotating");
}
//detect if the player release the button
else if (other.gameObject.CompareTag("hook") && Input.GetKeyUp(KeyCode.UpArrow))
{
rotationStarted = false;
rb.AddForce(transform.forward * speed); //let the vehicle moves linearly again
Debug.Log("Going to be linear");
}
}
// DISABLED: it does not works as expected although this --> FOR SECURITY REASONS: in the case the vehicle's collider shouldn't touch the hook anymore while keeping pressed the button, the vehicle stops; this function prevent this bug letting the vehicle going linear
/*private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("hook"))
{
rotationStarted = false;
rb.AddForce(transform.forward * speed);
Debug.Log("DOH! Problems with colliders, I'm going to be linear");
}
}
*/
void Update()
{
}
}
As you can probably note, there are also little problems of other type but for the moment it would be awesome to understand how to rotate the vehicle in order to make it tangent to hook.
Thanks in advance!

