So I am trying to make a collision function for my main camera so that when it hits 1 of 2 in game props it will change its y position. Essentially I’m using this so that when my camera hits a high level object it raises its position so that the camera will adjust to the height needed to view the object. The code is simple, or at least I thought it was. But it doesn’t work. This is what I had. Any ideas or tips?
It’ll be “reading” it, but it’s not calling it for whatever reason. Are you sure that your objects are actually colliding? Do they have Colliders and Rigidbodies attached as required?
Yeah, I attached colliders to them and they do restrict my camera’s access to pass through them. But for some reason I can’t get code to recognize that collision for whatever reason.
I thought maybe it had something to do with the objects are made up of two objects each. A wall and a gate. But they work as far as collision is concerned.
Oh, atm they do not. They only have the collision since they’re only a static object right now. Just tried adding that and it caused the object to disappear totally in the scene when played. Gonna have to look into the Rigibody component.
You probably want isKinematic = true and useGravity = false in the Inspector, hence “properly configured”. But yes, definitely go look them up, and read up on the built in physics system in general, it will be very useful for you.
Okay, so far I’ve managed to get it to work to a degree. I first added a Rigidbody with no gravity and isKinematic=false plus the collider to my camera controller. I then added the same to my walls. I then made a tag for those walls and called for them in the collision process.
//collision function for when the camera hits high points and needs to rise up higher over the object/terrain
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.tag == "Wall")
{
gameObject.Find("Camera Controler").transform.position.y = 18.80197;
} else
gameObject.Find("Camera Controler").transform.position.y = 0.6801915;
}
Now when I enter the game and the camera controller collides with the wall it raises up to the required y axis. However, it doesn’t drop back down. So working on that part right now. So far though the rigidbody did help considerably. Thanks for the input!
To grt the camera to fall, you will need to save the camera’s y position as resetPosition. After this create onCollisionExit () and tell the camera to go back to original position. Best to run the transition in update so it smoothly moves back to resetPosition just using the increment of the distance from new Y to reset Y.
you can also use rayCast if you are following behind an object to make sure the cameras view is not obstructed by other objects inbetween. This is more complicated to work out though, not terrible.
Yeah, I was trying to make it so when the camera’s controller hits the wall it (which would obscure it) would raise it’s y axis to be above the object. Then when it isn’t hitting that object it lowers back down to it’s original position.
I get what you mean about it falling apart now. Because the function is on a collision it can’t really reset because it’s not colliding with anything.
Surreal, that makes sense and goes with what penguin said. Gotta have the onCollisionExit for the reset. Will look into the rayCast because I’m thinking I may have the camera object obstruction quite a bit in the layout of my game. Didn’t expect it until I realized how big some of the models are gonna be.
Still haven’t figured out a good way to get this to work. I’ve started my script over and tried doing two collision functions. One on enter and the other on exit. I set a variable that defines the camera’s original position. I then set another variable called isHitting and made it false at the start. When the object hits another object under the tag “Wall” it changes the isHitting to true, and in an update function it determines whether isHitting is true or not and sets the y position based off the results.
First problem I found with this was that when it’s hitting I had the y position raise up too high and cause it to not hit. This would cause a continuous cycle of it changing the variables and y position. So I made it raise up enough so that it would still be hitting, but without being blocked in it’s vision.
Now though it’s stopped working entirely again. At this point I’m at a loss as I can’t seem to get passed a simple hittest. Gonna take a break and look into the rayCast.
I would suggest using a SphereCast downwards from your camera. This way you can simulate a spherical collider for the camera while it still above the gates.
I would also suggest using OnTriggerEnter instead of OnClliderEnter as to not have the camera act like an obtrusive object to the rest of the world.
If the camera trigger enters a gate raise it to the wanted height (setting a bool).
If bool is true, SphereCast downwards from the camera, if the cast returns false the camera is no longer above the gate (reset your bool and height of camera)
I’d do something similar. Instead of trying to detect collisions with specific objects and handle them specially, just define a height for your camera. Each frame cast downwards from the camera to see how high it is above whatever happens to be below it and adjust its height accordingly.
Yeah, that’s the direction I’m going with it now. From what I’ve read so far I’m thinking it’s the smoother way to go, especially since I’m going to have several camera adjustments through a map.
Ugh, at the moment I’m having trouble understanding the raycasts and spherecasts. From what I’ve read it’s like drawing a line from two points? Not sure if I got that right or not. But I’m having trouble understanding each thing within the function examples despite the brief explanations. But an example is
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
}
}
So my first look at this is that it’s creating a variable called “fwd” and making it equal a direction? The if statement I’m assuming is checking to see if it’s hitting anything in the transform direction in a distance of 10 and printing the text if true?
It’s drawing a line from a point in a direction and seeing if it hits anything. If it does hit something you can find out at what point along the line it hit, and what object it hit.
The “forward” is the direction of the line, so in your case it might be safe to simply use “new Vector3(0, -1, 0)”, which just points straight down in world space.