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 williampigmeu · Aug 01, 2012 at 09:17 PM · camerafpsheightcrouch

Camera Height-based crouch script

Hi, I want an way to make this: When I press CTRL, the Camera height decrease smoothly, and when I release, it increase smoothly too, because I have tools in front of the camera, and when I use other scripts, it deforms the tools.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ScroodgeM · Aug 01, 2012 at 09:20 PM

left/right shift - run, C - crouch

should be attached to FPC

RunAndCrouch.cs

using UnityEngine; using System.Collections; public class RunAndCrouch : MonoBehaviour { public float walkSpeed = 3; // regular speed public float crchSpeed = 1; // crouching speed public float runSpeed = 6; // run speed public float crchDeltaHeight = 1f; // height that crouched down public AnimationCurve crchHeightAnimation = new AnimationCurve(new Keyframe[] { new Keyframe(0f, 0f), new Keyframe(0.5f, 0.3f), new Keyframe(1f, 1f) });

 private CharacterMotor charMotor;
 private CharacterController charContr;
 private Transform _transform;
 private float crouchPhase;
 private float initHeight;

 void Start()
 {
     _transform = transform;
     charMotor = GetComponent();
     charContr = GetComponent();
     initHeight = charContr.height;
 }

 void FixedUpdate()
 {
     float crouchHeightOld01 = crchHeightAnimation.Evaluate(crouchPhase);
     crouchPhase = Mathf.MoveTowards(crouchPhase, Input.GetKey(KeyCode.C) ? 1f : 0f, Time.fixedDeltaTime);
     float crouchHeightNew01 = crchHeightAnimation.Evaluate(crouchPhase);
     float speed = Input.GetKey(KeyCode.C) ? crchSpeed : ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && charMotor.grounded) ? runSpeed : walkSpeed;

     charMotor.movement.maxForwardSpeed = speed; // set max speed
     charMotor.movement.maxSidewaysSpeed = speed;
     charMotor.movement.maxBackwardsSpeed = speed;

     charContr.height = initHeight - crouchHeightNew01 * crchDeltaHeight;
     charContr.center = Vector3.Lerp(Vector3.zero, Vector3.up * crchDeltaHeight * 0.5f, crouchHeightNew01);
     transform.position -= Vector3.up * (crouchHeightNew01 - crouchHeightOld01) * crchDeltaHeight;
 }

}

Comment
Add comment · Show 2 · 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 williampigmeu · Aug 01, 2012 at 09:49 PM 0
Share

But this is Zoom, I want to make the FPS character crouch, not the character itself, just change the "height" of the camera, something like moving it Y axis.

avatar image ScroodgeM · Aug 01, 2012 at 10:36 PM 0
Share

check answer again

avatar image
1

Answer by midomido · Aug 01, 2012 at 11:02 PM

 var speed: float = 2.5;
 var cameraPos: Vector3;  //this is the target position
 var cameraOldPos = transform.position //this is the old position or the current 
 function Update () {
     if (Input.GetButtonDown ("Fire1")) {
   transform.position = Vector3.Lerp(cameraOldPos, cameraPos, speed*Time.deltaTime);
     }
  }

and you can use GetButtonUp to restore it

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

8 People are following this question.

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

Related Questions

how can i make my character jump? 5 Answers

Camera Bobbing and Stairs 4 Answers

FPS camera script - keeps reverting to 0,0,0 rotation 1 Answer

I need help with my script 3 Answers

enter a trigger to access other camera? 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