- Home /
Raycast problems!
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;
}
}
your code always casts a ray down,, no matter what the rotation of the transform. You should try transform.up
ins$$anonymous$$d of Vector3.up
this doesnt seem to have worked at all, really dont understand as i copied the tutorials code yet it doesnt work and it worked on the tutorial
What version of Unity are they using? Things have changed over time. Are you sure that you did everything like the tutorial?
there using unity 3 im using the latest unity to make it, is there that much of a difference with the code? if i take out the slerp section it seems to let me rotate again when i come off a ramp but it still doesnt rotate the character to vertical shape of the ramp.
Answer by Benproductions1 · Apr 03, 2013 at 04:08 AM
OK, so reading you code, I realise the answer:
They are doing Quaternion Interpolation, using Lerp and I guess they are calculating it wrong. Instead the code they use, try this:
//At the start of your script
//Just defining the variables here, for both typecasting and optimisations
var targetDir:Vector3;
var ray:Ray;
var hit:RaycastHit;
//Inside the Update function
//Cast the ray relative to player
ray = Ray(trans.position + trans..up, trans.down);
//If we are on the ground and we get a raycasthit
if (Physics.Raycast(ray, hit, 100)) {
if (character.isGrounded) {
targetDir = hit.normal;
}
}
//Smoothly move towards the new direction
transform.up = Vector3.Slerp(transform.up, targetDir, 5 * Time.deltaTime);
Hope this helps,
Benproductions1
hi there just getting round to trying this it doesnt seem to turn my player with the ramp, im getting a NullReferenceException: Object reference not set to an instance of an object Skater Controller.Update () (at Assets/Standard Assets/Scripts/Own Scripts/Skater Controller.js:40)
//Cast the ray relative to player
ray = Ray(trans.position + trans.up, trans.down);
if (Physics.Raycast(ray, hit, 100)) {
if (character.isGrounded) {
targetDir = hit.normal;
so i dont know if this would be stopping it from working?
thanks
i out a debug.log above the line it says and it gave me this
Origin: (0.0, 0.0, 0.0), Dir: (0.0, 0.0, 0.0) UnityEngine.Debug:Log(Object) Skater Controller:Update() (at Assets/Standard Assets/Scripts/Own Scripts/Skater Controller.js:40)
i put the ray var in the debug.log and it said that.
does that mean that there is no ray line?