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 /
avatar image
0
Question by markfrancombe · Aug 03, 2012 at 11:55 PM · smooth follow

Smooth follow on WoW camera.. help?

NON-coder Alert!!

OK so I want my camera to move nicely behind my charater smoothly like with the built in SmoothFollow.js script.

Problem is I also want to be able to use the mouse to look about and steer (On mouse down.. NOT using MouseLook)

So I have this WoW camera script (credited as by matrix211v1)

This is almost perfect, except I wish it had that nice smooth movement that SmoothFollow gives (no I cant use both... tried...)

Can someone tell me which bit of SmoothFollow is doing this nice moving thing, cos some of it lokks very simerlar to a few lines in the WoW camera script...

For your delectation Script 1. SmoothFollow

 // 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;
 
 // Place the script in the Camera-Control group in the component menu
 @script AddComponentMenu("Camera-Control/Smooth Follow")
 
 
 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;
  
  // 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
  currentRotation = Quaternion.Euler (0, currentRotationAngle, 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);
 }

And now Script2. My Wow Camera.js (Im not sure if all of this is nessasary, I tried to strip away some stuff that a partner added...

 //WowCamera.js
 
 //When the camera hits something, it "instantly" gets behind the player.
 
 //Credits to this script: "matrix211v1"
 
 var target : Transform;
 
 //not sure what JD is doing here… leave it... for now...
 private var dataBase;
 
 var targetHeight = 3.0;
 var distance = 7.0;
 var maxDistance = 20;
 var minDistance = 2.5;
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 var zoomRate = 10;
 
 var rotationDampening = 3.0;
 
 var theta2 : float = 0.5;
 
 private var x = 0.0;
 private var y = 0.0;
 
 private var fwd = new Vector3();
 private var rightVector = new Vector3();
 private var upVector = new Vector3();
 private var movingVector = new Vector3();
 private var collisionVector = new Vector3();
 private var isColliding : boolean = false;
    
 private var a1 = new Vector3();
 private var b1 = new Vector3();
 private var c1 = new Vector3();
 private var d1 = new Vector3();
 private var e1 = new Vector3();
 private var f1 = new Vector3();
 private var h1 = new Vector3();
 private var i1 = new Vector3();
 
 function Start () {
     var angles = transform.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 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")) {
       var targetRotationAngle = target.eulerAngles.y;
       var currentRotationAngle = transform.eulerAngles.y;
       x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    }
    
    distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
    distance = Mathf.Clamp(distance, minDistance, maxDistance);
    
    y = ClampAngle(y, yMinLimit, yMaxLimit);
    
    var rotation:Quaternion = Quaternion.Euler(y, x, 0);
    var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
    
    // Check to see if we have a collision
    collisionVector = AdjustLineOfSight(transform.position, position);
    
    // Check Line Of Sight
    if (collisionVector != Vector3.zero)
    {
       Debug.Log("Check Line Of Sight");
        a1 = transform.position;
       b1 = position;
       c1 = AdjustLineOfSight(transform.position, position);
       d1 = c1 - a1;
       e1 = d1.normalized * -1;
       f1 = d1 + e1 * 1;
       g1 = f1 + a1;
       position = g1;
 
       // check distance player to camera
       h1 = position - a1;
       if (h1.magnitude < 10)
       {
          position = a1 - fwd * 4;
          //position.y = targetPlayer.y;
          theta2 = theta2 + .25;
       }
      
       // set new camera distance
       h1 = position - a1;
       distance = h1.magnitude;
    }
 
     //check collision
    if (Physics.CheckSphere (position, .5) )
    {
        a1 = transform.position;
      
       newPosition = a1 - fwd * 4;
       //newPosition.y = targetPlayer.y;
       theta2 = theta2 + .25;
      
       // set new camera distance
       h1 = position - a1;
       distance = h1.magnitude;
    }   
    
    position = Vector3.Slerp(transform.position, position, Time.deltaTime * 100);   
    
    transform.rotation = rotation;
    transform.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);
 }
 
 function AdjustLineOfSight (vecA: Vector3, vecB: Vector3)
 {
       var hit: RaycastHit;
    
       if (Physics.Linecast (vecA, vecB, hit))
       {
 //         Debug.Log("I hit something");
 //         return hit.point;
       }
      
      return Vector3.zero;
 }

REALLY hope someone can help!!

Cheers... MArk

Comment
Add comment · Show 2
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 Maulik2208 · Mar 26, 2013 at 02:29 PM 0
Share

Best of luck dude...... might someone will have enough time to read the whole code and figure out your problem with Proper Solution.........Cheers....

avatar image Unitraxx · Mar 26, 2013 at 02:31 PM 0
Share

Look at the date, it's not relevant anymore. Someone just revived it, because he wasn't able to use the code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UTeam · Mar 26, 2013 at 02:02 PM

I have a quick question.

How do i translate this line into c#:

 var wantedRotationAngle = target.eulerAngles.y;

;)

Comment
Add comment · Share
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
0

Answer by Ziron999 · Feb 12, 2014 at 11:33 AM

 var wantedRotationAngle = target.eulerAngles.y;

=

 float wantedRotationAngle = target.eulerAngles.y;


 transform.position.y = currentHeight;

=

 transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Change hieght on smooth follow OnTriggerEnter 0 Answers

How to make animated animal's head smoothly look at player instead of instantly snapping? 1 Answer

Mouse orbit + Smooth follow script question 0 Answers

How to make animated animal's head smoothly look at player instead of instantly snapping? 1 Answer

Setting Target of SmoothFollow Script After Initiating Prefab 1 Answer


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