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 12, 2012 at 04:21 PM · cameraraycastterrain

Inserting UnityScript Raycasting code into C# script ?

hey , heres my camera script in C# ..

 using UnityEngine;
 using System.Collections;
 
 public class CarCamera : MonoBehaviour
 {
     public Transform target = null;
     public float height = 1f;
     public float positionDamping = 3f;
     public float velocityDamping = 3f;
     public float distance = 50f;
     private Vector3 prevVelocity = Vector3.zero;
     private Vector3 currentVelocity = Vector3.zero;
     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;
                 
         transform.position = newPosition;
         transform.LookAt(newTargetPosition);
     
         }
 }


//// And im trying to insert this Raycasting script inside it but its in UnityJavascript and for the life of me I cannot seem to combine the two without errors :(

 function Update () {
  
    var up = transform.TransformDirection(Vector3.up);
    var hit : RaycastHit;    
    Debug.DrawRay(transform.position, -up * 10, Color.green);
  
    if(Physics.Raycast(transform.position, -up, hit, 10)){
       Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "floor"){
       Destroy(GetComponent(Rigidbody));
       }
    }
 }

I just want the camera to rise up to stop it going through the terrain ,as two seperatye scripts attached to main camera it works ( I get the raycast warning) but cannot raise the camera up ,(I presume one script is overpowering the other ?) Can anyone help with this please I am totally stuck here ,I seem to have tried everything but failed :(

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 javanoob · Dec 12, 2012 at 11:40 PM 0
Share

function Update () {

var up = transform.TransformDirection(Vector3.up); var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 10, Color.green);

if(Physics.Raycast(transform.position, -up, hit, 10)){ Debug.Log("Hit");
if(hit.collider.gameObject.name == "floor"){ Destroy(GetComponent(Rigidbody)); } } }

If someone could convert this to C# that would be a massive help :) I know what I need to do just keep getting syntax mess ups :( the scripts work together nicely :)

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

O$$anonymous$$G lol the obvious just clicked ^^ I guess I need to read a value from the raycaster script to tell the cameras $$anonymous$$ Y position to be in the carcamera script ,If thats correct I feel like such an Uber numpty if Im right ,I'll get back tou you all ,brain is fried but I rhink im on it lol :)

avatar image CodeMasterMike · Dec 13, 2012 at 06:24 AM 0
Share

Try this for C# conversion. I haven't checked if it works more than it doesn't give compilation errors:

 Vector3 up = transform.TransformDirection(Vector3.up); 
 RaycastHit hit;
 Debug.DrawRay(transform.position, -up * 10, Color.green);

 if(Physics.Raycast(transform.position, -up, out hit, 10.0f))
 { 
     Debug.Log("Hit");
     if(hit.collider.gameObject.name == "floor")
     { 
      Destroy(GetComponent<Rigidbody>()); 
     } 
  } 
avatar image javanoob · Dec 13, 2012 at 03:55 PM 0
Share

hmm I get this ?

Assets/llll.cs(1,9): error CS0116: A namespace can only contain types and namespace declarations.

Btw will this work ?

I guess I need to read a value from the raycaster script to tell the cameras $$anonymous$$ Y position to be in the carcamera script ,grab a value from raycaster script ,if true then raise camera in carcamera script ? . Is it this obvious or am I missing something lol ?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by javanoob · Dec 15, 2012 at 09:27 PM

Lol it turns out all this time one of my camera scripts was freezing the rotation values in other scripts,even when it was not attached ^^ .So I got there in the end :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
avatar image
1

Answer by MibZ · Dec 13, 2012 at 04:48 PM

You most likely can't raise your camera because your movement code relies on the velocity variable of its rigidbody, but you are destroying it if the camera is less than 10 units above the floor.

If you want to restrict movement you should do it with a variable, not by removing components.

 Vector3 up = transform.TransformDirection(Vector3.up); 
 RaycastHit hit;
 Debug.DrawRay(transform.position, -up * 10, Color.green);
 
 if(Physics.Raycast(transform.position, -up, out hit, 10.0f))
 { 
     Debug.Log("Hit");
     if(hit.collider.gameObject.name == "floor")
     { 
          canMove = false;
     } 
 } 
 
 
 //Add something like 'bool canMove' to your script and add this if statement before you assign your new position
 if (canMove)
     transform.position = newPosition;
 
 
Comment
Add comment · Show 12 · 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 13, 2012 at 08:10 PM 0
Share

Im completely lost lol ,everytime I try to add anything the the Carcamera script I get various errors :(

I tried adding this bit

// remove these lines first ? // using UnityEngine; //using System.Collections; //public class example : $$anonymous$$onoBehaviour { // remove these lines first ?

public ScriptName script; void Example() { script = GetComponent("ScriptName") as ScriptName; script.DoSomething(); } }

And I get errors ,I just wanted to read a value from the raycaster script because adding to the C# script for me seems impossible ,I have tried 20 different things but it will not accept any extra code :( .I am truly lost now and thinking giving up is the best option :( (maybe an in car view ins$$anonymous$$d) lazt way out but I am totally lost ,I have tried sphere colliders attached to the camera and nothing seems to wotk ?is this a Unity bug with colliders ?

All I want to do is add collison detection to the Car Camera script from the Unity Car Tutorial ,and everything im trying is failing ,the closest I got was the two seperate scripts approach .Anyone have a clue how to do this ?

avatar image MibZ · Dec 13, 2012 at 08:53 PM 0
Share

Files don't just refuse code being added, and if you want to learn how to code giving up isn't a good option.

To start with, do you have a script named "ScriptName"?

avatar image javanoob · Dec 13, 2012 at 08:54 PM 0
Share

using UnityEngine; using System.Collections;

 public class CarCamera : $$anonymous$$onoBehaviour
 {
     bool can$$anonymous$$ove;
     
     
     public Transform target = null;
     public float height = 1f;
     public float positionDamping = 3f;
     public float velocityDamping = 3;
     public float distance = 50f;
     public Layer$$anonymous$$ask ignoreLayers = -1;
 
     private RaycastHit hit = new RaycastHit();
 
     private Vector3 prevVelocity = Vector3.zero;
     private Layer$$anonymous$$ask 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 = $$anonymous$$athf.Clamp01(target.root.rigidbody.velocity.magnitude / 170.0f);
         camera.fieldOfView = $$anonymous$$athf.Lerp(55, 72, speedFactor);
         float currentDistance = $$anonymous$$athf.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;
         
 
         Vector3 up = transform.TransformDirection(Vector3.up); 
 // RaycastHit hit;
 Debug.DrawRay(transform.position, -up * 10, Color.green);
 
 if(Physics.Raycast(transform.position, -up, out hit, 10.0f))
 { 
     Debug.Log("Hit");
     if(hit.collider.gameObject.name == "floor")
     { 
          can$$anonymous$$ove = false;
     } 
 } 
 
 
 //Add something like 'bool can$$anonymous$$ove' to your script and add this if statement before you assign your new position
 if (can$$anonymous$$ove)
     transform.position = newPosition;
         
         
 
          // transform.position = new Vector3(transform.position.x, 7f, transform.position.z);   //added c codr that worked 
         
         transform.position = newPosition;
         transform.LookAt(newTargetPosition);
                 
         
         }
 }


Thank you :)I managed to get the code into the carcamera script and it registers a hit when the raycast hits :) just got to work out how to raise the camera slowly now :) ,BUt thanks 10000000x trying to get the Java into the C# was driving me nuts lol :p Im hoping the next bit will be easy now its all in one script :)

avatar image MibZ · Dec 13, 2012 at 08:59 PM 0
Share

No problem! Learning program$$anonymous$$g is difficult, especially when you're trying to convert one language to another. (Especially when it's from JavaScript to something else :p) $$anonymous$$eep at it dude!

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

Thanks $$anonymous$$ibz ,im getting it slowly and loving it :) Ever since I was 12 years Old I wanted to make AAA games ,and Unity plus people like you make it all possible :) I'm a 3D artist by hobby and I really want to make something nice :)

Show more comments
avatar image
0

Answer by javanoob · Dec 15, 2012 at 09:32 PM

All this time one of my scripts was somehow messing with the others ^ ^ no wonder it took a while to pin down :) Merry Christmas guys :)

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 javanoob · Mar 19, 2013 at 07:33 PM 0
Share

Its a random thing ,I give up :( months of random issues mean I am just never gonna be good at this .

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

RTS camera help 1 Answer

Raycasting on camera corners 3 Answers

How to get Camera Collision Detection on Terrain 1 Answer

Ray camera to Terrain goes Wrong. 1 Answer

Make the terrain ignore Raycast if in between Camera and Player? 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