im making a simple skateboard game with the design3 tutorial. the raycast code i have copied does not seem to work. i want it to tilt the skater as he goes up a vertical ramp to make it look realistic. i am a beginner in coding and even more of a beginner on raycasts. i dont know if my code is wrong or there is just something wrong with my model i am using. when ever i go up a ramp it doesnt turn then when i go back down the ramp it wont let me rotate the character left or right untill im on another ramp then i can turn the character but not on the flat plane for some reason. hope this all makes sense.
My code.
var rotateSpeed = 90;
var pushImpulse = 3.5;
var maxSpeed = 12;
var decayRate = 0.1;
private var character : CharacterController;
private var trans : Transform;
private var speed = 0.0;
private var targetRot : Quaternion;
function Start ()
{
character = GetComponent(CharacterController);
trans = transform;
}
function Pushing()
{
speed += pushImpulse;
speed = Mathf.Min(speed, maxSpeed);
}
function Update ()
{
var horizontal = Input.GetAxis("Horizontal");
trans.Rotate(0,rotateSpeed * horizontal * Time.deltaTime,0);
if (character.isGrounded && Input.GetKey(KeyCode.UpArrow))
Pushing();
var moveDirection = trans.forward * speed;
moveDirection += Physics.gravity;
character.Move(moveDirection * Time.deltaTime);
var ray = new Ray(trans.position + Vector3.up, -Vector3.up);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100))
{
targetRight = Vector3.Cross(hit.normal, trans.forward);
targetForward = Vector3.Cross(targetRight, hit.normal);
if ( character.isGrounded )
targetRot = Quaternion.LookRotation(targetForward, hit.normal);
}
trans.rotation = Quaternion.Slerp(trans.rotation, targetRot, 5 * Time.deltaTime);
if (character.isGrounded)
{
//Stops character when slowed down enough
if (speed < 0.3)
speed = 0;
else
//simple code to add friction to character
speed -= decayRate * Time.deltaTime * speed;
}
}