Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Adelphia · Apr 02, 2013 at 04:42 PM · rotationraycastbeginnerraycasthitskateboard

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;
     }
 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 02, 2013 at 11:27 PM 1
Share

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

avatar image Adelphia · Apr 03, 2013 at 12:27 AM 0
Share

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

avatar image Benproductions1 · Apr 03, 2013 at 12:32 AM 0
Share

What version of Unity are they using? Things have changed over time. Are you sure that you did everything like the tutorial?

avatar image Adelphia · Apr 03, 2013 at 01:12 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adelphia · Apr 07, 2013 at 05:38 AM 0
Share

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

avatar image Adelphia · Apr 07, 2013 at 05:39 AM 0
Share

also line 40 is the ray = Ray line

avatar image Benproductions1 · Apr 07, 2013 at 08:54 AM 0
Share

Can you debug which part of that line it is?

avatar image Adelphia · Apr 07, 2013 at 05:14 PM 0
Share

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)

avatar image Adelphia · Apr 07, 2013 at 06:41 PM 0
Share

i put the ray var in the debug.log and it said that.

does that mean that there is no ray line?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make 2 object not go into each other when modifying one object's position 0 Answers

Using raycasters to freeze enemies 1 Answer

Rotating to Slope 1 Answer

Raycasting and changing player rotation accoording to what is underneath. 0 Answers

default cylinder to indicate hit.normal 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges