Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 08, 2020 at 04:08 PM by axellolledoering for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by axellolledoering · Jan 23, 2020 at 08:20 AM · rotationvector3raycasthit

How do I rotate an object based on the terrain?

I am making a kart game like MarioKart and I am shooting 4 rays down from the kart. I have 2 problems.

1: When I shoot the raycasts down with Vector3.down, it shoots downward, but not downward from the kart's rotation, which is what I need.

2: I want to rotate the kart based on the average of the hitInfos normals, but when I try this it says it can't convert a Vector3 to a float.

Basically I want the kart to rotate based on the terrain. If anyone could help me with my problems it would be great!

Thanks!

This is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class KartDriving : MonoBehaviour { public Transform kart; public Vector3 hitInfoAverageNormal;

 public Vector3 rayFrontStart;
 public Vector3 rayLeftStart;
 public Vector3 rayBackStart;
 public Vector3 rayRightStart;

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     rayFrontStart.Set(transform.position.x, transform.position.y - transform.localScale.y / 2, transform.position.z + transform.localScale.z / 2);
     rayLeftStart.Set(transform.position.x - transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2, transform.position.z);
     rayBackStart.Set(transform.position.x, transform.position.y - transform.localScale.y / 2, transform.position.z - transform.localScale.z / 2);
     rayRightStart.Set(transform.position.x + transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2, transform.position.z);
     StabalizeKart();
 }


 void StabalizeKart()
 {
     //Here we shoot the raycasts underneath the kart
     //------------------------------------------------------------------------------------------------

     RaycastHit hitInfoFront;
     RaycastHit hitInfoLeft;
     RaycastHit hitInfoBack;
     RaycastHit hitInfoRight;

     //Here we shoot the raycasts
     Physics.Raycast(rayFrontStart, Vector3.down, out hitInfoFront, 100);
     Physics.Raycast(rayLeftStart, Vector3.down, out hitInfoLeft, 100);
     Physics.Raycast(rayBackStart, Vector3.down, out hitInfoBack, 100);
     Physics.Raycast(rayRightStart, Vector3.down, out hitInfoRight, 100);

     //Here we debug the data stored from the raycasts
     if (Input.GetKey(KeyCode.Space))
     {
         Debug.DrawRay(rayFrontStart, Vector3.down);
         Debug.Log(hitInfoFront.distance);

         Debug.DrawRay(rayLeftStart, Vector3.down);
         Debug.Log(hitInfoLeft.distance);

         Debug.DrawRay(rayBackStart, Vector3.down);
         Debug.Log(hitInfoBack.distance);

         Debug.DrawRay(rayRightStart, Vector3.down);
         Debug.Log(hitInfoRight.distance);
     }

     //Here we use the data from the raycasts to rotate the kart
     //------------------------------------------------------------------------------------------------
     hitInfoAverageNormal.Set(hitInfoFront.normal + hitInfoBack.normal / 2, transform.position.y, hitInfoLeft.normal + hitInfoRight.normal / 2);
     kart.rotation = Quaternion.FromToRotation(Vector3.up, hitInfoFront.normal);
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1

Answer by rufopufo · Jan 23, 2020 at 11:36 AM

Hi there,

First of all, instead of shooting the rays in direction Vector3.down, you must use the player's down vector. This is made by calling: -transform.up (as transform.down does not exist, this is the same).

Probably this also helps in the second question. If it does not work, just use Quaternion.FromToRotation(transform.up, hitInfoFront.normal) instead.

Hope it helps.

Comment
Add comment · Show 1 · 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 axellolledoering · Feb 02, 2020 at 07:37 PM 0
Share

Thanks for your response. It does work. Now I just need to find out how to fix problem 2.

Follow this Question

Answers Answers and Comments

200 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate an object so its up vector matches a sphere 1 Answer

Creating the After Effects Wiggle expression to effect rotation in C# 1 Answer

Aligning/rotating vectors 1 Answer

Why does this code not work? 1 Answer

Boxcast vs Raycast Oddities 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