- Home /
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?
Start by changing Physics.Raycast(this.transform, -Vector3.up, out hit)
to Physics.Raycast(transform.position, -Vector3.up, out hit)
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'
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
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.
If anyone wonders, its actually:
transform.position = new Vector3(hit.point.x, hit.point.y + HeightfromGround , hit.point.z);
Thanks for the script!
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.
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.
Your answer
Follow this Question
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