- Home /
Camera raycast?
hey all! i was doing peteys from burgzergarcades tutorial series camera part and when he made his camera he didnt add collision. i was wondering if you guys wanted to help me figure this out i want the camera to collide with everything in the game and i heard some people using raycast. i am noobie at scripting and never done raycasting but this is it and sorry if it gets cut off half way through dont know hot to fix that :
using UnityEngine; using System.Collections;
public class RPGCamera : MonoBehaviour { public Transform target; public float walkDistance; public float runDistance; public float height; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float heightDamping = 2.0f; public float rotationDamping = 3.0f;
private Transform _myTransform; private float x; private float y; private bool _camButtonDown = false;
void Awake() { _myTransform = transform;
} // Use this for initialization void Start () { if(target == null) { Debug.LogWarning("We do not have a target for the camera"); } else { CameraSetup(); } }
void Update() { if(Input.GetMouseButtonDown(1)) _camButtonDown = true; if(Input.GetMouseButtonUp(1)) _camButtonDown = false; }
void LateUpdate() { if(target != null) {
if(_camButtonDown) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
// y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else {
// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // _myTransform.LookAt(target); x = 0; y = 0;
// Calculate the current rotation angles float wantedRotationAngle = target.eulerAngles.y; float wantedHeight = target.position.y + height;
float currentRotationAngle = _myTransform.eulerAngles.y;
float currentHeight = _myTransform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
// Set the height of the camera
_myTransform.position = new Vector3 (_myTransform.position.x, currentHeight, _myTransform.position.z);
// Always look at the target
_myTransform.LookAt (target);
}
}
}
public void CameraSetup() { _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); _myTransform.LookAt(target);
}
}
Answer by The_r0nin · Jan 16, 2011 at 07:19 PM
If you want the camera to collide, simply add a collider and a rigidbody. That way it will collide with the other objects in the scene. Unless you wanted some other specific behavior that isn't clear from your question...
That doesnt work with a camera. and rigidbody makes it fall, with the behavior i want, i want raycast do that if you hit it, it will bump forward alittle bit and if it collides with like a house or something it will go straight to the back of the player, adding a rigidbody just makes it fall to the ground...
Click is$$anonymous$$inematic under the rigidbody, and it will only move when it is scripted to move, but it will still collide and send OnCollision info, so you can script your behavior from there...
I mean even if i do that it will still go through because i am forcing it through and it not do anything because if you click and drag it will still go through.
You have to script the reactions you want. In the OnCollision function, you script how it will behave. That's what you would have to do with a raycast, but raycast will eat up more processing. If there is any other way to do what you want without scripting... I don't know it...
Lol, i just dont think that it will work because even if i do try that it still dont work,but thanks for the help man.
Answer by JJNCreator · Aug 13, 2012 at 04:35 PM
Easy. Make a private float called distanceToGround. Put this code in the Update function:
if (Physics.Raycast(_myTransform.position, -Vector3.up, out hit, 100.0f)) {
distanceToGround = hit.distance;
Then, times the walk distance in the Vector3 position variable by distanceToGround. Then you shouldn't have any errors. You might want to edit my code as I spent all day working on it and it isn't perfect, but it's good enough for now.
(if post doesn't come out right, please edit).
Your answer
Follow this Question
Related Questions
3rd Person camera collision 2 Answers
Camera collision detection. 0 Answers
Camera collision with raycasting 1 Answer
Ray Cast not working for camera. 1 Answer
Can someone modify this for me? 0 Answers