- Home /
Terrain Collider Bug
Hey! So Ive got a terrain and use my camera to control but whenever I run into the hill it doesnt use phycics and lets me go through it. Ive looked into it and it says save the scene but that doesnt work.... Anyone got a answer.
Whenever I run my camera. I use my camera to control and when its going into the hill it literally goes into it. Sorry. I should of been more precise
$$anonymous$$ore than likely this is not a bag, but a misunderstanding.
How are you moving your camera? Actual code being used would be helpful. Does your camera have a collider?
If your camera has no collider, OR is being moved without physics, then it will never "collide" without an outside force that IS using physics.
$$anonymous$$y camera doesnt have a colider and which one would I use if I add one. Also here is my movement script
[code] using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WASD$$anonymous$$ove : $$anonymous$$onoBehaviour {
public float ScrollSpeed = 15;
public float ScrollEdge = 0.1f;
public float speed = 4;
public float PanSpeed = 10;
public Vector2 zoomRange = new Vector2( -10, 100 );
public float CurrentZoom = 0;
public float ZoomZpeed = 1;
public float ZoomRotation = 1;
public Vector2 zoomAngleRange = new Vector2( 20, 70 );
public float rotateSpeed = 10;
private Vector3 initialPosition;
private Vector3 initialRotation;
void Start () {
initialPosition = transform.position;
initialRotation = transform.eulerAngles;
}
void Update () {
// panning
if ( Input.Get$$anonymous$$ouseButton( 0 ) ) {
transform.Translate(Vector3.right * Time.deltaTime * PanSpeed * (Input.mousePosition.x - Screen.width * 0.5f) / (Screen.width * 0.5f), Space.World);
transform.Translate(Vector3.forward * Time.deltaTime * PanSpeed * (Input.mousePosition.y - Screen.height * 0.5f) / (Screen.height * 0.5f), Space.World);
}
else {
if ( Input.Get$$anonymous$$ey("d") ) {
transform.Translate(Vector3.right * speed * Time.deltaTime * PanSpeed, Space.Self );
}
else if ( Input.Get$$anonymous$$ey("a") ) {
transform.Translate(Vector3.right * speed * Time.deltaTime * -PanSpeed, Space.Self );
}
if ( Input.Get$$anonymous$$ey("w") ) {
transform.Translate(Vector3.forward * speed * Time.deltaTime * PanSpeed, Space.Self );
}
else if ( Input.Get$$anonymous$$ey("s") ){
transform.Translate(Vector3.forward * speed * Time.deltaTime * -PanSpeed, Space.Self );
}
if ( Input.Get$$anonymous$$ey("q") ) {
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed, Space.World);
}
else if ( Input.Get$$anonymous$$ey("e") ) {
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);
}
}
// zoom in/out
CurrentZoom -= Input.GetAxis("$$anonymous$$ouse ScrollWheel") * Time.deltaTime * 1000 * ZoomZpeed;
CurrentZoom = $$anonymous$$athf.Clamp( CurrentZoom, zoomRange.x, zoomRange.y );
transform.position = new Vector3( transform.position.x, transform.position.y - (transform.position.y - (initialPosition.y + CurrentZoom)) * 0.1f, transform.position.z );
float x = transform.eulerAngles.x - (transform.eulerAngles.x - (initialRotation.x + CurrentZoom * ZoomRotation)) * 0.1f;
x = $$anonymous$$athf.Clamp( x, zoomAngleRange.x, zoomAngleRange.y );
transform.eulerAngles = new Vector3( x, transform.eulerAngles.y, transform.eulerAngles.z );
}
} [/code]
Well there is your problem. You need to attach a collider to the camera, any collider would work. Then you need to attach a rigidbody to it, and move the object with physics via the rigidbody with Velocity, AddForce, or $$anonymous$$oveTowards.
2 Things. $$anonymous$$y terrain does have rigidbody so I will get rid of that but how would I move the camera without transform.translate? PS Ran out of replys so made a new comment
Well I wrote a tutorial on Velocity-based movement. Velocity-Based $$anonymous$$ovement 101
You can easily alter it to use AddForce if you prefer that as well. You'll get the hang of this sort of thing :) We all started somewhere, but try and do a bit of googling before asking any questions, even if you don't find an answer, it will help your Google-Fu, which is really one of our biggest assets as programmers.
Your answer
Follow this Question
Related Questions
Unity 5 no terrain collision 1 Answer
Can't crash airplane into ground 4 Answers
Camera Colliding with Terrain 1 Answer
Airplane - Collision With Terrain Problem 1 Answer
Stopping character from walking on grass 0 Answers