want help in camera script witch i want the camera collides with terrain and don’t go through it
to watch the sample player
www.remnum.com/test.html
Use raycasting and check distance to terrain. if y of camera is lower than hit.point just move the camera to point.y or a bit over.
thank you for helping but i wrote some code but it doesn’t stop camera at ground it make camera stuck;
if (Physics.Raycast (transform.position, -Vector3.up, hit))
{
if(transform.position.y < hit.point.y)
{
transform.position.y = hit.point.y; // or = hit.point.y + 5;
}
}
i have a similar camera script, i simply decrease the distance from the target when it is collideing (and i dont care the hit.point). This way it is a bit smoother.
if you want it simple, try:
transform.position.y +=Time.deltaTime*2; for example.
here is my whole code, if you are interested, part of it is from this forum:
var target : Transform;
private var myTransform:Transform;
var targetHeight :float= 0.5;
var targetRight :float= 0.0;
var distance :float= 2.0;
private var prevButtonRight:boolean=false;
var maxDistance :float= 20;
var minDistance :float= 2;
var xSpeed :float= 250.0;
var ySpeed :float= 120.0;
var yMinLimit :float= -20;
var yMaxLimit :float= 80;
var zoomRate :float= 1;
var rotationDampening :float= 3.0;
var theta2 : float = 0.5;
private var x :float= 0.0;
private var y :float= 0.0;
private var fwd :Vector3= new Vector3();
private var rightVector :Vector3= new Vector3();
private var upVector :Vector3= new Vector3();
private var movingVector :Vector3= new Vector3();
private var collisionVector :Vector3= new Vector3();
private var isColliding : boolean = false;
private var distmod :float=0.0;
function Start () {
myTransform=transform;
var angles :Vector3= myTransform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if(!target)
return;
if (Input.GetMouseButtonUp(0)) prevButtonRight=false;
if (Input.GetMouseButtonUp(1)) prevButtonRight=true;
// If either mouse buttons are down, let them govern camera position
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
// otherwise, ease behind the target if any of the directional keys are pressed
} else if((Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))&prevButtonRight) {
var targetRotationAngle = target.eulerAngles.y;
var currentRotationAngle = myTransform.eulerAngles.y;
x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * zoomRate * Mathf.Abs(distance);// * Time.deltaTime
distance = Mathf.Clamp(distance, minDistance, maxDistance);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation:Quaternion = Quaternion.Euler(y, x, 0);
var targetMod : Vector3=Vector3(0,-targetHeight,0) - (rotation*Vector3.right*targetRight);
var layerMask = 1<<8;
layerMask = ~layerMask;
var position = target.position - (rotation * Vector3.forward * (distance-distmod) + targetMod);
var position2= target.position - (rotation * Vector3.forward * (0.1) + targetMod);
// Check to see if we have a collision
if ((Physics.CheckSphere (position, .4, layerMask)||Physics.Linecast (position2, position, layerMask))(distmod<distance))
{
//position = target.position - (rotation * Vector3.forward * (distance-distmod) + Vector3(0,-targetHeight,0));
distmod=Mathf.Lerp(distmod,distance,Time.deltaTime*2);
}
else
{
var newdistmod=Mathf.Lerp(distmod,0.0,Time.deltaTime*2);
if (newdistmod<0.1) newdistmod=0.0;
if (!Physics.CheckSphere (target.position - (rotation * Vector3.forward * (distance-newdistmod) + targetMod), .4, layerMask)!Physics.Linecast (position2, target.position - (rotation * Vector3.forward * (distance-newdistmod) + targetMod), layerMask)(distmod!=0.0)){
distmod=newdistmod;
}
}
//position = Vector3.Slerp(transform.position, position, Time.deltaTime * 100);
myTransform.rotation = rotation;
myTransform.position = position;
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
this is my camera move code
you solve it in good way and i thinks it solve my problem
but there is must script can do the main target " camera collides the terrain and set on ground smooth"
thank u for your code
// The target we are following
var target : Transform;
var TopZoomLimit = 60.0;
var MinZoomLimit = 10.0;
// The distance in the x-z plane to the target
var distance = 30.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceToGround = 0.0 ;
private var x = 0.0;
private var y = 0.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow and Zoom")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
transform.LookAt (target);
}
function Update () {
// Early out if we don't have a target
if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
{
distance++;
if(distance >= TopZoomLimit)
{
distance = TopZoomLimit;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
{
distance--;
if(distance <= MinZoomLimit)
{
distance = MinZoomLimit;
}
}
if (target Input.GetButton ("Fire2")) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
followtarget();
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
function followtarget()
{
var hit : RaycastHit;
var rotation = Quaternion.Euler(y, x, 0 );
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
if (Physics.Raycast (transform.position, Vector3.up, hit))
{
position = hit.point ;//+ Vector3(1,1,1);
print(position);
}
if (Physics.Raycast (transform.position, -Vector3.up, hit))
{
if(position.y <= hit.point.y)
{
distance--;
print(position);
}
}
transform.rotation = rotation;
transform.position = position;
}