Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 20, 2013 at 02:51 PM by AlucardJay for the following reason:

Duplicate Question : http://answers.unity3d.com/questions/496868/can-someone-modify-this-for-me.html

avatar image
0
Question by smirlianos · Jul 20, 2013 at 02:46 PM · cameracollisionraycastraycasthit

How can I change this script?

Hello!

I found this script for camera movement. It has many functions, and one is to prevent camera from going through walls.

What must I erase so it has this effect only?

Thanks!

 using UnityEngine;
 using System.Collections;
  
 public class WowCamera : MonoBehaviour
 {
  
 public Transform target;
  
 public float targetHeight = 1.7f;
 public float distance = 5.0f;
  
 public float maxDistance = 20;
 public float minDistance = .6f;
  
 public float xSpeed = 250.0f;
 public float ySpeed = 120.0f;
  
 public int yMinLimit = -80;
 public int yMaxLimit = 80;
  
 public int zoomRate = 40;
  
 public float rotationDampening = 3.0f;
 public float zoomDampening = 5.0f;
  
 private float x = 0.0f;
 private float y = 0.0f;
 private float currentDistance;
 private float desiredDistance;
 private float correctedDistance;
  
 void Start ()
 {
 Vector3 angles = transform.eulerAngles;
 x = angles.x;
 y = angles.y;
  
 currentDistance = distance;
 desiredDistance = distance;
 correctedDistance = distance;
  
 // Make the rigid body not change rotation
 if (rigidbody)
 rigidbody.freezeRotation = true;
 }
  
 /**
 * Camera logic on LateUpdate to only update after all character movement logic has been handled.
 */
 void LateUpdate ()
 {
 // Don't do anything if target is not defined
 if (!target)
 return;
  
 // If either mouse buttons are down, let the mouse govern camera position
 if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
 {
 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 }
 // otherwise, ease behind the target if any of the directional keys are pressed
 else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
 {
 float targetRotationAngle = target.eulerAngles.y;
 float currentRotationAngle = transform.eulerAngles.y;
 x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
 }
  
 y = ClampAngle(y, yMinLimit, yMaxLimit);
  
 // set camera rotation
 Quaternion rotation = Quaternion.Euler(y, x, 0);
  
 // calculate the desired distance
 desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
 desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
 correctedDistance = desiredDistance;
  
 // calculate desired camera position
 Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));
  
 // check for collision using the true target's desired registration point as set by user using height
 RaycastHit collisionHit;
 Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
  
 // if there was a collision, correct the camera position and calculate the corrected distance
 bool isCorrected = false;
 if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
 {
 position = collisionHit.point;
 correctedDistance = Vector3.Distance(trueTargetPosition, position);
 isCorrected = true;
 }
  
 // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
 currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
  
 // recalculate position based on the new currentDistance
 position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));
  
 transform.rotation = rotation;
 transform.position = position;
 }
  
 private static float ClampAngle(float angle, float min, float max)
 {
 if (angle < -360)
 angle += 360;
 if (angle > 360)
 angle -= 360;
 return Mathf.Clamp(angle, min, max);
 }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 20, 2013 at 02:52 PM 2
Share

I closed the first question as you are just asking for someone to do the work for you. This is not what Unity Answers is about.

Don't then post the same question again.

Duplicate Question : http://answers.unity3d.com/questions/496868/can-someone-modify-this-for-me.html

Sorry, but your question is not suitable for Unity Answers. Please use the Unity Forum for discussions such as "How to ...". Unity Answers is here to help you solve any specific problems you have.

  • http://forum.unity3d.com/

  • http://answers.unity3d.com/page/newuser.html

  • http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image smirlianos · Jul 21, 2013 at 12:21 PM 0
Share

I don't want anyone to do the work for me. I just need to know what parts of the code are necessary for the effect I want and I will do the work by myself...

avatar image IgorAherne · Jul 21, 2013 at 12:37 PM 1
Share

lines 75 to 105 is what it is about, make sure you have the required variables declared

avatar image smirlianos · Jul 22, 2013 at 10:17 AM 0
Share

thanks, that's what I wanted :)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fall collision force with help of raycast 0 Answers

3rd Person camera collision 2 Answers

Camera collision detection. 0 Answers

Raycasting to detect which side of collider is hit 2 Answers

Unity raycast not working / returning weird value 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges