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
0
Question by javanoob · Mar 19, 2013 at 08:09 PM · cameracartutorial

Carcamera script raycasting fail ?

Can anyone point out why the Unity car tutorial carcamera raycasting fails please ?

 using UnityEngine;
 using System.Collections;
 
 public class CarCamera : MonoBehaviour
 {
     public Transform target = null;
     public float height = 1f;
     public float positionDamping = 3f;
     public float velocityDamping = 3;
     public float distance = 50f;
     public LayerMask ignoreLayers = -1;
 
     private RaycastHit hit = new RaycastHit();
 
     private Vector3 prevVelocity = Vector3.zero;
     private LayerMask raycastLayers = -1;
     
     private Vector3 currentVelocity = Vector3.zero;
     
     void Start()
     {
         raycastLayers = ~ignoreLayers;
     }
 
     void FixedUpdate()
     {
         currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
         currentVelocity.y = 0;
         prevVelocity = currentVelocity;
     }
     
     void LateUpdate()
     {
         float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 170.0f);
         camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
         float currentDistance = Mathf.Lerp(45.5f, 6.5f, speedFactor);
         
         currentVelocity = currentVelocity.normalized;
         
         Vector3 newTargetPosition = target.position + Vector3.up * height;
         Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
         newPosition.y = newTargetPosition.y;
         
         Vector3 targetDirection = newPosition - newTargetPosition;
         if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
             newPosition = hit.point;
         
         transform.position = newPosition;
         transform.LookAt(newTargetPosition);
                 
         
         }
 }

I am wondering why the raycasting doesn't stop the camera going through the terrain or other objects on this script ,anyone have any ideas ,I presume because its the Unity car tut it works for everyone else ,I have been stuck with the same issue for months now no matter what camera scripts I try ,please can anyone tell me whats wrong as I dont want to have to make all race tracks flat lol.

Comment
Add comment · Show 6
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 · Mar 20, 2013 at 01:04 AM 0
Share

I'm guessing it's because your setting the camera too close to the collision point. Try: newPosition = hit.point + hit.normal;

avatar image javanoob · Mar 20, 2013 at 12:25 PM 0
Share

Camera still goes underground,I am completely stumped with this,can I ask does it work for you ? Im getting a 1 in a million error maybe :(

A lot of people suggest this approach ins$$anonymous$$d musing Terrain sample height ?

height = Terrain.activeTerrain.SampleHeight(transform.position) + Terrain.activeTerrain.transform.position.y;

Would it be this obvious and forget raycasting ,which just doesnt seem to work for me anyway ?

avatar image javanoob · Mar 20, 2013 at 12:32 PM 0
Share

Is there one Unity Dev on here that could at least tell me if this is possible or is it not hence the Unity Car Tutorial camera will always go through the floor ,I have serched and tried fixing this for six months and I just cannot seem to get a working solution ?

avatar image javanoob · Mar 22, 2013 at 12:45 AM 0
Share

I actually emailed the Unity $$anonymous$$m and guess what ,they sent me back here lol ,Is Camera versus Terrain just not possible ,I cannot seem to find one tutorial that mentions this anywhere ? Are all unity games destined for the camera to drop through the floor ,please please someone tell me if it is at least possible so I dont waste any more time trying to achieve the impossible .

avatar image Benproductions1 · Mar 22, 2013 at 01:17 AM 0
Share

Everything is possible, EVERYTHING. Nothing is impossible, it just depends on how long you are willing to spend on doing anything. So, yes it is possible. Can you describe in more detail as to what happens, and it what situations you arn't getting the result you are looking for?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by javanoob · Mar 24, 2013 at 03:12 AM

Got it :) used the terrain height command ,obvious really lol ,I am such a Javanoob :p

Comment
Add comment · 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

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

Unity Car Tutorial Error 0 Answers

UNEXPECTED TOKEN ) 1 Answer

Unity Car tutorial Brake 1 Answer

Unity Car Tutorial Error 0 Answers

Question about rigidbody velocity 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