- Home /
slope detection
i am creating a game where my character called "hero" is running on a Terrain. when i was using a plain train it was working fine ,problem starts when i want to run the same character on slopes i have box colider in both of legs of character. during slope ,first portion of colider help my character to run. but running is not natural because on slope it must tilt little bit and that is not happening code is as follows
static var monster : GameObject ;
private var ray : Ray;
private var hit : RaycastHit;
static var speed : int;
function Start ()
{
monster = GameObject.Find("hero");
speed= 10;
}
function Update ()
{
monster.rigidbody.velocity.x = speed;
}
//collision detection
var isCollideTurning : boolean;
isCollideTurning = true;
function OnCollisionEnter ()
{
monster.animation.Play("run");
}
Answer by robertbu · Aug 07, 2013 at 03:23 PM
Raycasting() is used to detect the slope. If you are going just for the slope of the terrain, then I suggest you use Colider.Raycast() rather than Physics.Raycast(). Sample code:
var hit : RaycastHit;
var ray = new Ray(transform.position, Vector3.down);
if (someCollider.Raycast(ray, hit)) {
var slope = hit.normal;
//Adjust character based on normal
}
This gives you back the normal to the slope:
Your question was unclear about how you wanted to adjust the rotation of your character. A lot of scripts I see here adjust the object to align with the normal. You can do that by:
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
i am having the problem with the object and train ,for example when u drive the car in hills ur car is not parallel (it tilt according to slope/angle) with ur ground as compare to normal plane driving i have uploaded the video just check it out and plz help u can see player is running straight
Did you add the code I suggested? Putting both pieces together it would be:
var hit : RaycastHit;
var ray = new Ray(transform.position, Vector3.down);
if (someCollider.Raycast(ray, hit, 1000)) {
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
}
Where someCollider is a reference to the collider on your terrain.
there is error inn the if statement where u told me to put the terrain collide saying
An instance of type 'UnityEngine.colider' is required to access non static member 'raycast'.
what is the problem
At the top of your script, put:
var someCollider : Collider;
Then in the inspector, drag the terrain onto this variable.
The best overload for the method 'UnityEngine.Collider.Raycast(UnityEngine.Ray, ref UnityEngine.RaycastHit, float)' is not compatible with the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit)'.
can u give me ur mail id so that i can chat with u this is very $$anonymous$$or problem and m stuck in it for 2 days my project will not go any further until this problem is solved
Answer by Xtro · Aug 07, 2013 at 02:00 PM
I think you should use capsule collider. To prevent you character to laydown, you can freeze the rotation on selected axes.
thanks Xtro but when we increase the drag in the rigidbody component ewverything works fine
Answer by Saddamjit_Singh · Sep 04, 2017 at 09:25 AM
This code is having a problem.
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) transform.rotation*;
multiplying the equation with transform.rotation changes y axis of gameobject slowly over time.
Your answer
Follow this Question
Related Questions
Sliding game slope jittering. 0 Answers
Gyroscope Tilting Rotation. Quaternions T_T Help. 1 Answer
Maze game style rotation 1 Answer
Skateboarding Rotation Problem 1 Answer