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
1
Question by Tatsunomi · May 29, 2013 at 04:46 AM · cs1502cs1503cs1612

Make the camera Rise with terrain height C#

I'm a little bit confused on how to make the camera hover on terrain to work. I've been recieving errors CS1503 ~ CS1502.

 using UnityEngine;
 using System.Collections;
 
 
 
 public class CamHover : MonoBehaviour {
     
     RaycastHit hit;
     
     
     [SerializeField]
     float HeightfromGround = 50;

     // Use this for initialization
     void Start () {
     
 
     }
     
     // Update is called once per frame
     void Update () {
 
      if(Physics.Raycast(transform.position, -Vector3.up, out hit))
         if (hit.transform.tag == ("ground")){
             transform.position.y = 
                 hit.point.y + HeightfromGround;
 }
     }
 }


What am I doing wrong?

Comment
Add comment · Show 2
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 jerobinson · May 29, 2013 at 06:06 AM 0
Share

Start by changing Physics.Raycast(this.transform, -Vector3.up, out hit) to Physics.Raycast(transform.position, -Vector3.up, out hit)

avatar image Tatsunomi · May 29, 2013 at 12:31 PM 0
Share

I edited a few parts of the code by adding:

 Vector3 yTransform = transform.position;

 void Start () {

     transform = yTransform;
 
     }

and replaced:

  transform.position.y =
 hit.point.y + HeightfromGround;

with:

 yTransform.position.y = hit.point.y + HeightfromGround;

After that I get an error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

3 Replies

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

Answer by Statement · May 29, 2013 at 12:41 PM

In general, if an error is generated for you and you don't understand it, take some time to read it through properly and if you can't figure it out, try googling the error code or error message.

CS1612: Cannot modify the return value of 'expression' because it is not a variable.

transform.position.y = hit.point.y + HeightfromGround;

You can't write to transform.position.y because transform.position returns a copy of the position vector, and writing to a (temporary) copy of the position makes no sense. What you need to do is probably something like...

transform.position += new Vector3(0, hit.point.y + HeightfromGround, 0);

...where you get and set the position vector.

The docs on MSDN describes this as:

   // Invalid because ValueProp returns a value on the stack,
   // and although you could set that value,
   // it doesn't reflect back into the original property.
   e.ValueProp.Prop = 8;   // CS1612

Comment
Add comment · Show 2 · 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 Tatsunomi · May 29, 2013 at 01:30 PM 1
Share

Thank you for pointing that out sir. Since I wanted my camera to hover over the terrain at a certain height so I wouldn't want it to fly to infinity and stay locked in x=0 & z=0 every single time the raycast hits the "ground". So I changed last the last part to:

 transform.position = new Vector3(hit.point.x, hit.point.y + HeightfromGround , hit.point);

And it Finally Hovers at a given height which is 50. Thanks again kind sir.

avatar image HenryChinaski · Jul 16, 2015 at 10:41 PM 1
Share

If anyone wonders, its actually:

  transform.position = new Vector3(hit.point.x, hit.point.y + HeightfromGround , hit.point.z);
 

Thanks for the script!

avatar image
0

Answer by bigbat · May 29, 2013 at 12:52 PM

I'm working on a open world 3rd person project that use a camera script like skyrim.It is fully working and smart that avoid Barriers and hover terrain.that part of my code is:

         Vector3 position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset  );
         RaycastHit hit;
         int layermask;  
         /*
          * we set player to layer 8,so we bitshift layermask to 8 and negative it,
          * so we dont collide with player and all in same layer.and collide with anything in other layers
          * */
         layermask=1<<8;
         layermask = ~layermask;
           /*
          *we check whethere we can see player from current camera position,if we cant see player
           *and anything is in front of camera so we reposition camera next to that thing
           */
         if(Physics.Linecast(target.position,position,out hit,layermask))
         {
          position=new Vector3(hit.point.x,hit.point.y,hit.point.z-cameraavoidcollider);    
          Debug.Log(hit.collider.gameObject.name);    
         }
 
               //now we trace a ray downward from camera
         if(Physics.Raycast(position,transform.TransformDirection(-Vector3.up),out hit))
         { // if camera position is very near to a collider like terrain or i.e a building we  reposition camera to afew top of it
             //Debug.Log("direction:"+transform.TransformDirection(-Vector3.up)+" hited/////hitpoint:"+hit.point+"----------diff:"+hit.point.y+0.2f);
             if(Vector3.Distance(position,hit.point)<=0.2)
             {
                 position=new Vector3(hit.point.x ,hit.point.y+aboveground,hit.point.z);
             }
         }
         /*basically when we trace a ray from camera position doward,we must hit something like terrain
          so if we dont collide with anything it means we are under earth(terrain) base on new rotation
          therefore we trace a ray upward to collide with terrain and reposition camera above hit point; 
         */
         else if(Physics.Raycast(position,transform.TransformDirection(Vector3.up),out hit))
         {
             position=new Vector3(hit.point.x,hit.point.y+aboveground, hit.point.z);
             //Debug.Log("we are under earth----direction:"+transform.TransformDirection(Vector3.up)+" hited");
         }
         /*we check distance 0.5 meter in front of camera to determine whether any thing is there.
          if we collide with any thing  we reposition camera to 0.4 meter above hit point 
         */
         if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out hit,CameraFrontBorder))
         {
             position=new Vector3(position.x ,hit.point.y+aboveground,position.z);
         }
         
 
         transform.position = position;

I hope this is helpful to you.

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 casanueva · Jul 15, 2015 at 10:47 AM

Hello!

Came across this problem to, had a class with a member variable which i could not directly change. My solution was to make a Set function so that the class changed the variable it self. It's easier to read and its good practice to make this.

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

18 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

Related Questions

error CS1502 and CS1503 1 Answer

ERROR CS0199, ERROR CS1502, ERROR1503 1 Answer

Error CS1525 calling OntriggerEnter 1 Answer

cannot destroy transform 1 Answer

CS1502 and CS1503 Error 0 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