- Home /
Aligning player to surface
I am currently making an vehicle combat game and I'm struggling to write a script for my vehicles. The player controls a hovering vehicle that sits only a small distance above the ground and rotates on the X and Z axis to match the terrain angles. Almost like it sticks to the surface.
The way I had planned this was to cast a ray down from my rigidbody vehicle to the ground and measure the distance between them. The Y axis height is set to a variable and the ray ensures that the height stays at the variable.
The ray also checks the ground angle and moves the vehicle to match it, but the vehicle cannot climb 90 degree angles.
I understand what I am trying to do, but I am unsure on how this should be set out in a JavaScript (I haven't worked with raycasting/rigidbodies that often)
A similar example would be dodgem arena on the ps1 Link
Answer by Cherno · Jul 14, 2015 at 06:28 AM
This should get you started:
//declare the variables that are needed
public bool grounded;
private Vector3 posCur;
private Quaternion rotCur;
void Update() {
//declare a new Ray. It will start at this object's position and it's direction will be straight down from the object (in local space, that is)
Ray ray = new Ray(transform.position, -transform.up);
//decalre a RaycastHit. This is neccessary so it can get "filled" with information when casting the ray below.
RaycastHit hit;
//cast the ray. Note the "out hit" which makes the Raycast "fill" the hit variable with information. The maximum distance the ray will go is 1.5
if(Physics.Raycast(ray, out hit, 1.5f) == true) {
//draw a Debug Line so we can see the ray in the scene view. Good to check if it actually does what we want. Make sure that it uses the same values as the actual Raycast. In this case, it starts at the same position, but only goes up to the point that we hit.
Debug.DrawLine(transform.position, hit.point, Color.green);
//store the roation and position as they would be aligned on the surface
rotCur = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
posCur = new Vector3(transform.position.x, hit.point.y, transform.position.z);
grounded = true;
}
//if you raycast didn't hit anything, we are in the air and not grounded.
else {
grounded = false;
}
if(grounded == true) {
//smoothly rotate and move the objects until it's aligned to the surface. The "5" multiplier controls how fast the changes occur and could be made into a seperate exposed variable
transform.position = Vector3.Lerp(transform.position, posCur, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime * 5);
}
else {
//if we are not grounded, make the object go straight down in world space (simulating gravity). the "1f" multiplier controls how fast we descend.
transform.position = Vector3.Lerp(transform.position, transform.position - Vector3.up * 1f, Time.deltaTime * 5);
//from memory, I'm not sure why I aded this... Looks like a fail safe to me. When the object is turned too much towards teh front or back, almost instantly (*1000) make it rotate to a better orientation for aligning.
if(transform.eulerAngles.x > 15) {
turnVector.x -= Time.deltaTime * 1000;
}
else if(transform.eulerAngles.x < 15) {
turnVector.x += Time.deltaTime * 1000;
}
//if we are not grounded, make the vehicle's rotation "even out". Not completey reaslistic, but easy to work with.
rotCur.eulerAngles = Vector3.zero;
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime);
}
}
Ok, just converted this to javascript and set turnvector to a Vector3 variable and the script works well, the vehicle matches the angles of the terrain. The only problem I'm having is with the "hovering" part. The vehicle doesn't sit above the ground but rather inside it. How do I fix this?
Try adding to the posCur's y value:
posCur = new Vector3(transform.position.x, hit.point.y + 1f, transform.position.z);
That fixed it, but I have one little issue.
The vehicle tends to align too slowly, often resulting in the whole object clipping out of the track.
Can I just fix that by adjusting a certain variable?
In these lines of code, the multiplier by which time.deltaTime gets multiplied (5) can be changed to your liking. I suggest using a seperate variable for it which you can change from the main body during runtime via the inspector or script.
transform.position = Vector3.Lerp(transform.position, posCur, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime * 5);
HI there @Cherno I just want to thank you for the help!
I am new to game development and had a similar problem and this thread really helped me a lot, not only to solve my problem but to learn new techniques and the coding behind your solution. ($$anonymous$$ight I suggest adding comments next time, since I would have really liked to know what each line of code actually does, but I kinda figured it out and commented it myself).
Again thank you so much for helping out it really helped me a lot!
Good to hear that it was a big help to you. Yes, after taking another look at my code, I think some comments would have been good. I will try to keep that in $$anonymous$$d for the next time. Thanks for the suggestion.
Answer by Saddamjit_Singh · Sep 04, 2017 at 07:48 AM
Do this --
using UnityEngine;
public class SlopeDetectionScript : MonoBehaviour
{
RaycastHit hit;
Quaternion rot;
public int smooth;
void Update()
{
Ray ray = new Ray(transform.position, -transform.up);
if (Physics.Raycast(ray, out hit))
{
rot = Quaternion.FromToRotation(transform.up, hit.normal) *
transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, rot,
Time.deltaTime * smooth);
}
}
}
Your answer
Follow this Question
Related Questions
Aligning an Object to a Surface, and Rotating it 2 Answers
Need help with limiting rotation. 1 Answer
Rigidbody.AddForce with Raycast? 1 Answer
Input problem 1 Answer