• 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 $$anonymous$$t = 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 $$anonymous$$t, currentDistance, raycastLayers))
             newPosition = $$anonymous$$t.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 t$$anonymous$$s 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
avatar image javanoob · Mar 20, 2013 at 12:25 PM 0
Share
avatar image javanoob · Mar 20, 2013 at 12:32 PM 0
Share
avatar image javanoob · Mar 22, 2013 at 12:45 AM 0
Share
avatar image Benproductions1 · Mar 22, 2013 at 01:17 AM 0
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Camera turns to the front when reversing car 1 Answer

Shadows are not continuous and only when i go near they be shown. 0 Answers

Roll-a-ball tutorial does not work. 1 Answer

car tutorial' camera adjustment problem 2 Answers

How to make a camera screen on my camera model? 2 Answers


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