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 · Dec 09, 2012 at 01:28 AM · c#cameraterrain

Any ideas as to why I get this error 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;
     
     private void Awake()
     {
         camera.transform.parent = target;
     }
  
     private void FixedUpdate() 
     {
         Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
  
         // check to see if there is anything behind the target
         RaycastHit hit;
         Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward); 
  
         // cast the bumper ray out from rear and check to see if there is anything behind
         if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
             && hit.transform != target) // ignore ray-casts that hit the user. DR
         {
             // clamp wanted position to hit position
             wantedPosition.x = hit.point.x;
             wantedPosition.z = hit.point.z;
             wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
         } 
  
         transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
  
         Vector3 lookPosition = target.TransformPoint(targetLookAtOffset);
  
     
     
     
     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);
                 
     
         
         }
 }



this is a combination of two scripts ,im trying to mix the Car Camera script from the Unity Car tutorial with a Terrain and object aware script I found on unity wiki ,but I get this error .Any ideas please ?

Assets/Scripts/CSharpScripts/CarCamera.cs(57,23): error CS1547: Keyword `void' cannot be used in this context

Comment
Add comment · Show 1
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 javanoob · Dec 09, 2012 at 02:17 PM 0
Share

Ideally I would just love to make the smoothfollowcamerawithbumper script follow the car with some side to side movement like the first script ,if that makes sense (you turn left right you see the sides of the car while turning ) ins$$anonymous$$d of a fixed rigidly to the back view ..Probably something obvious im missing :P

I added as an answer because my brain is truly fried after abouy two months trying to sort this out :(

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by T27M · Dec 09, 2012 at 01:32 AM

You missed a closing bracket on the first FixedUpdate(). You also have FixedUpdate() twice which I think is not possible, unless you meant that to be Update().

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

Answer by Bunny83 · Dec 09, 2012 at 01:33 AM

Sure, Your FixedUpdate function is missing it's closing bracket. So the compiler sees that you declared your Start function inside another function but that's not possible.

Next thing is you have two FixedUpdate functions. That's also not possible. just merge them into one by taking the content of one of them and paste it inside the other.

Btw, what's the point of merging two scripts? The great thing about components is that you can keep them simple and just attach them to the object you want. If they do different things, keep them seperated, but that's up to you.

Comment
Add comment · Show 3 · 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 javanoob · Dec 09, 2012 at 07:25 PM 0
Share

I want the soothfollow camera off the car camera script with the terrain aware function of the other script ,if i try to attach both to the camera the car camera makes the second script non functional ,thats why im trying (very unsuccesfully lol) to merge the two

im practically begging now lol this is just evading me ,yet I believe it should be simple :(

avatar image javanoob · Dec 11, 2012 at 01:30 PM 0
Share

Could anyone tell me if its at least possible ,what im trying or am I goona be banging my head against a brick wall for even longer lol ? please :)

avatar image javanoob · Dec 11, 2012 at 11:11 PM 0
Share

Im gonna try raycasting ,I think thats the way to go :)

avatar image
0

Answer by javanoob · Dec 10, 2012 at 04:10 AM

 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;
     
     
     private float bumperDistanceCheck = 2.5f; // length of bumper ray
   private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
  private float damping = 5.0f;
     private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
  
     private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
     
     private bool smoothRotation = true;
     
     private float rotationDamping = 10.0f;
  
     
     
     
     void Start()
     {
         raycastLayers = ~ignoreLayers;
     }
 
     void FixedUpdate()
     {
         currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
         currentVelocity.y = 0;
         prevVelocity = currentVelocity;
            
         Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
  
         // check to see if there is anything behind the target
         RaycastHit hit;
         Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward); 
  
         // cast the bumper ray out from rear and check to see if there is anything behind
         if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
             && hit.transform != target) // ignore ray-casts that hit the user. DR
         {
             // clamp wanted position to hit position
             wantedPosition.x = hit.point.x;
             wantedPosition.z = hit.point.z;
             wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
         } 
  
         transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
  
         Vector3 lookPosition = target.TransformPoint(targetLookAtOffset);
  
         if (smoothRotation)
         {
             Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
             transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
         } 
         else 
             transform.rotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
     }
 
     
     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);
                 
         
         }
 }


heres my mix up of the two scripts but i am totally losing the terrain collison thing now :)

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

Distribute terrain in zones 3 Answers

Distant terrain level of detail question. 0 Answers

Multiple Cars not working 1 Answer

Best way to Camera Pick multiple objects and move them together 2 Answers

camera collision visibility (c#) 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