Hey guys, I found a camera collision script that I like the only problem is the script is hooked up with a "var target : Transform" What I need it to do however is search for a tag target instead So this is the original code that I found here on the forums (I would have asked there but the thread was from like 2007, didn't wanna revive an old one). Code (csharp): // The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0; //---New code--- //how close does the camera have to get to a wall before it will bounce var bounceRange : float = 0.5; var bounceAmt : float = 20; //---End of new code--- function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height; currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y; //---New/altered code--- //Cast rays to the left and right of the camera, to detect walls. if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange)) { currentRotationAngle -= bounceAmt; } else if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange)) { currentRotationAngle += bounceAmt; //If no walls are detected, execute the normal follow behavior. } else { // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); } //---End of new code--- // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation // The quaternion interface uses radians not degrees so we need to convert from degrees to radians currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); } I tried adding the target tags myself but when I start the game up it doesn't go to the target. Here's my attempt at trying to fix it myself Code (csharp): // The target we are following var TargetTag : String = "Player"; private var target : GameObject; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0; //---New code--- //how close does the camera have to get to a wall before it will bounce var bounceRange : float = 0.5; var bounceAmt : float = 20; //---End of new code--- function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height; currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y; //---New/altered code--- //Cast rays to the left and right of the camera, to detect walls. if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange)) { currentRotationAngle -= bounceAmt; } else if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange)) { currentRotationAngle += bounceAmt; //If no walls are detected, execute the normal follow behavior. } else { // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); } //---End of new code--- // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation // The quaternion interface uses radians not degrees so we need to convert from degrees to radians currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; } So could someone help me fix it up? Thanks in advance.
It looks like you've defined a TargetTag string for the script but didn't really do anything with it, so it just sits there. Without purpose. Unappreciated and misunderstood. Left alone to stare into the void and silently ask itself: "Why?" ... You'll be wanting to pass it to the FindWithTag method, which will return a single active GameObject tagged with the matching string (or null if none are found, so you probably want to account for that). You can assign that to your target Transform err... that is, change your target GameObject back to a Transform type and assign it the .transform property of the result of the FindWithTag method. And you'll be probably be wanting to do that only once on Start. See if that doesn't get it working the way you want.
Ha xD that made me chuckle. I tried and failed to add what you said. errors came up whenever I added something and tried to fix tho but the same result came, the cam wouldn't lock onto the player (not a scripter). Thanks for the pointer though man, I appreciate it.
There were a few missing variable declarations in the script, but I think what you want is something like this: Code (csharp): #pragma strict //The target we are following var targetTag : String; var target : Transform; //The distance in the x-z plane to the target var distance = 10.0; //The height we want the camera to be above the target var height = 5.0; //How much we var heightDamping = 2.0; var rotationDamping = 3.0; //---New code--- //how close does the camera have to get to a wall before it will bounce var bounceRange : float = 0.5; var bounceAmt : float = 20; function Start() { if(targetTag != null target == null) { var taggedObject = GameObject.FindWithTag(targetTag); if(taggedObject != null) { target = taggedObject.transform; } } } //---End of new code--- function LateUpdate() { // Early out if we don't have a target if(!target) return; // Calculate the current rotation angles var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; //---New/altered code--- //Cast rays to the left and right of the camera, to detect walls. if(Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange)) { currentRotationAngle -= bounceAmt; } else if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange)) { currentRotationAngle += bounceAmt; } //If no walls are detected, execute the normal follow behavior. else { // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); } //---End of new code--- // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation // The quaternion interface uses radians not degrees so we need to convert from degrees to radians var currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); } The Start function implements the usage of FindWithTag that I had mentioned. I didn't look at the other details specific to this camera script, but it looks like it should work as intended now.
Hey man, totes didn't expect you to have fixed it up for me but thanks a lot for that. I'll give it a go and see how it works now.