Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tachikoma · Jun 19, 2011 at 03:41 AM · camerasmoothfollowstatic variable

some behavior on over-shoulder view and static var in update()

Hi, my game use third-person view.And in normal state camera will always follow player back.Now I have some problem in makeing the aim state. the situation is

  • When player press RMB,camera will move to over-shoulder view.(during this process,player can't move)

  • After camera reach the desired position, fix the camera view to player(which means camera always follow player in fixed position, now is lower left-corner on screen).

  • Then recover the player movement ability and mouse look.

  • Once release RMB, camera convert to normal view.

My problem is I use lerp to smooth camera when it convert to over-shoulder view.It seems to update every frame.I am not sure that how to realize that : If camera reached the over-shoulder view position,stop update the smooth function.I also has problem in getting variable from other script.Now I create one function to check the current distance and wanted distance of camera.If it didn't reached desired distance, use first follow function.Else use second follow function.I also create a ` static var canmove which can be accessed by playercontrolscript.The playerconrolscipt get the variable and change walk speed . But the variable in playercontrolscript seems not update every frame following my camera script variable.

I tried a lot method but still not work well.....Can someone give me any idea? Thanks..

edit (copied from comment and fixed indent)

 var target : Transform; 
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 var initDis = 10;
 var minDis = 2.0;
 var maxDis = 10.0;
 
 var currentdis ;
 var wheelSpeed = 5;
 
 static var x = 0.0;
 static var y = 0.0;
 
 static  var distance =5.0;
 var fixdis;
 private var position;
 private var rotation;
 var changeonce = 0;
 
 
 static var IsMoving =1;
 
 function Start ()
 {
     x = 30;
     y = 0;
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 private var smoothTime = 0.5;
 private var velocity = 0.0;
 
 private var onetime = 0;
 
 function Update () 
 {
     if (target) 
     {
         checkdis();
         currentdis = Vector3.Distance(target.position,transform.position);
  
         if(Input.GetMouseButton(1))
         {
             print("fixdis"+fixdis);
             
             if ((currentdis-fixdis)>0.07)
             {
                 IsMoving = 1;  //check if camera position approach the over-shoulder view
                 Overshoulder();
                 print("currentdis"+currentdis);
             }
             else if((currentdis-fixdis)<=0.07)
         {
                 IsMoving = 0;
                 print("Moving false");
                 
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
                 
                 y = ClampAngle(y, yMinLimit, yMaxLimit);
                 
                 rotation = Quaternion.Euler(y, x-10, 0);
                 position = rotation * Vector3(0.3, 1.8, -1.2)+ target.position;
                 transform.rotation = rotation;
                 transform.position= position;
                 target.rotation = Quaternion.Euler(0,x,0);
             }
         }
         else
         {
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
             rotation = Quaternion.Euler(y, x, 0);
             position = rotation * Vector3(0.5, 1.5, -2)+ target.position;
             
             transform.rotation = rotation;
             transform.position= position;
             target.rotation = Quaternion.Euler(0,x,0);
         }
     }
 }
 
 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 checkdis()
 {
     if(changeonce == 0)
     {
         rotation = Quaternion.Euler(y, x-15, 0);
         position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
         fixdis = Vector3.Distance(target.position,position);
         changeonce = 1;
     }
 }
 
 //smooth the camera moving to overshoulder view
 function Overshoulder()
 {
     rotation = Quaternion.Euler(y, x-15, 0);
     position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
     
     var newY = Mathf.SmoothDamp(0, position.x - transform.position.x, velocity, smoothTime);
     var factor = newY / (position.x - transform.position.x);
     
     transform.position += Vector3(Mathf.Lerp(0, position.x - transform.position.x, factor),
                                   Mathf.Lerp(0, position.y - transform.position.y, factor),
                                   Mathf.Lerp(0, position.z - transform.position.z, factor));
 }    
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 Merglasch · Jun 19, 2011 at 12:49 PM 0
Share

Would be useful to see some of the code you've written until now. Also why do you use to different scripts for you camera movement?

avatar image tachikoma · Jun 20, 2011 at 05:28 AM 0
Share

Actually I just start learning script ,I don't know well how to organize them. here is my code.Sorry for the orderless script..

var target : Transform;

var xSpeed = 250.0; var ySpeed = 120.0;

var y$$anonymous$$inLimit = -20; var y$$anonymous$$axLimit = 80;

var initDis = 10; var $$anonymous$$Dis = 2.0; var maxDis = 10.0;

var currentdis ; var wheelSpeed = 5;

static var x = 0.0; static var y = 0.0;

static var distance =5.0; var fixdis; private var position; private var rotation; var changeonce = 0;

static var Is$$anonymous$$oving =1;

function Start () {

x = 30; y = 0;

if (rigidbody) rigidbody.freezeRotation = true;

}

private var smoothTime = 0.5; private var velocity = 0.0;

private var onetime = 0;

function Update () {

 if (target) 
 {
 
    checkdis();

 currentdis = Vector3.Distance(target.position,transform.position);

 

if(Input.Get$$anonymous$$ouseButton(1)) {

 print("fixdis"+fixdis);

 if ((currentdis-fixdis)>0.07)
 {
  
      Is$$anonymous$$oving = 1;  //check if camera position approach the over-shoulder view
  Overshoulder();
 print("currentdis"+currentdis);
 }
 
 else if((currentdis-fixdis)<=0.07)
 {
   
   Is$$anonymous$$oving = 0;
   print("$$anonymous$$oving false");

    x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
    y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
   
    y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
    
   rotation = Quaternion.Euler(y, x-10, 0);

 position = rotation * Vector3(0.3, 1.8, -1.2)+ target.position;
  
    transform.rotation = rotation;
    transform.position= position;
    
 target.rotation = Quaternion.Euler(0,x,0);
 }
  

} else { x += Input.GetAxis("$$anonymous$$ouse X") xSpeed 0.02; y -= Input.GetAxis("$$anonymous$$ouse Y") ySpeed 0.02;

y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);

rotation = Quaternion.Euler(y, x, 0);

position = rotation * Vector3(0.5, 1.5, -2)+ target.position;

transform.rotation = rotation; transform.position= position;

target.rotation = Quaternion.Euler(0,x,0); }

}

}

static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max); }

function checkdis() { if(changeonce == 0) { rotation = Quaternion.Euler(y, x-15, 0); position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;

 fixdis = Vector3.Distance(target.position,position);
 changeonce = 1;
 }
 

}

//smooth the camera moving to overshoulder view function Overshoulder() {

 rotation = Quaternion.Euler(y, x-15, 0);
 position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
   
 var newY = $$anonymous$$athf.SmoothDamp(0, position.x - transform.position.x, velocity, smoothTime);
 var factor = newY / (position.x - transform.position.x);

 transform.position += Vector3($$anonymous$$athf.Lerp(0, position.x - transform.position.x, factor),
                                            $$anonymous$$athf.Lerp(0, position.y - transform.position.y, factor),
                                         $$anonymous$$athf.Lerp(0, position.z - transform.position.z, factor));
 
 
  
 

}

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Smooth Camera follow script is causing framerate drops when moving 1 Answer

How to make camera position relative to a specific target. 1 Answer

Space Game Camera 2 Answers

Camera zoom in and zoom out of the target with smoothfollow 0 Answers

SmoothFollow to play catchup to player position 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