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 PrimeDerektive · Nov 12, 2010 at 05:34 AM · cameralerp

Can't get lerping of camera positions to work properly

I'm using a modified version of the OrbitCamera from the 3rd person platformer tutorial. I'm trying to achieve a lock-on effect where the camera orbits around the player by default, but if you look at an object tagged enemy and tap the right mouse button, the camera position switches to the position of an empty gameobject positioned behind the player's shoulder called lockOnPos, and the camera rotation is locked to the player rotation. If you click the right mouse button again, you unlock, and it goes back to the orbit camera.

As you'll see in my code, I achieved this by using a coroutine to constantly poll whether or not "lockOnToggle" is true, and I toggle it by the Fire2 button (right mouse). If it is true, in Update, I cache the position of the camera as it was in orbit in a variable called prevCamPos, then switch the camera position to the position of the lockOnPos object, and lock the camera rotation to the player object. Otherwise, I check to see if prevCamPos was set in the previous update, and if it was, set the camera position back to prevCamPos, and then do the normal OrbitCamera stuff.

This works fine, but I don't like the way it snaps immediately, so I tried lerping the camera positions when I do the switches, and its completely jittery and weird, and I can't figure out why.

Anyway, here's the bulk of my code:

function Update () { if (player) {

 if(lockOnToggle == true)
 {
         prevCamPos = transform.position;
         player.gameObject.GetComponent(PlayerLockOn).LockOn(enemyPos);
         transform.position = lockOnPos.position;
         transform.rotation = player.rotation;
 }
 else
 {

     x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

     y = ClampAngle(y, yMinLimit, yMaxLimit);

     var rotation = Quaternion.Euler(y, x, 0);
     var targetPos = player.position + targetOffset;
     var direction = rotation * -Vector3.forward;

     var targetDistance = AdjustLineOfSight(targetPos, direction);
     currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);

     transform.rotation = rotation;
     newCamPos= targetPos + direction * currentDistance;

     if(prevCamPos == Vector3.zero)
         transform.position = newCamPos;
     else{
         LerpPosition(0.5, prevCamPos, newCamPos);
         prevCamPos = Vector3.zero;
     }

 }

}

}

function LerpPosition(time : float, prevPos : Vector3, newPos : Vector3) { var originalTime = time;

while (time > 0.0)
{
    time -= Time.deltaTime;
    transform.position = Vector3.Lerp(newPos, prevPos, time / originalTime);
    yield;
}

}

Any idea what I'm doing wrong?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Proclyon · Nov 12, 2010 at 10:14 AM

Currently your camera's previous position variable "prevCamPosprevCamPos" is set to the transform it was then. This is an absolute position. So if your character would move it and release lock to old form you would go back to that ABSOLUTE position instead of the relative position from the main character.

This point of origin is also the one you want to use for a Linear Interpolation (LERP), maybe the problem is the relative position and the calculation of the curve. That's just a wild speculation on my behalf though. You can quickly test this to see if there is difference when u move or not. If there is no difference it does not matter.

Perhaps what u need instead is a bad call or LERP, what is that time variable and how can it stop being called?

It could be something as simple as the LERP steps [Domain] simply being set to to high a value or that the camera is changing the lookAt spradically during the process.

I'm not sure this is even remotely a good answer, but idea's don't ever happen in White Space :)

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

No one has followed this question yet.

Related Questions

First Person Character, Mecanim. 1 Answer

Shader works on Android but it is black on iOS 1 Answer

Transition current camera rotation to 0,0,0 1 Answer

Camera.rect lerp not working 2 Answers

Changing camera position to shoot through scope on gun 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