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?

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

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.

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.