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 Jackson21 · Mar 03, 2015 at 12:18 PM · c#movementcharactercontrollerbeginnercrouch

Custom C# Crouch Script

Hey,

I know there are some similar questions out there. I have already looked int to them but most of them are written in Java- respective Unityscript, with which I' m quiet unfamiliar. So I come up with this one.

To get used to the scripting with Unity, I try to work out erverthing by myself. I'm currently working trough a C# book at the same time, so potentally the problem is the poor understanding by myself for this language.

I came up with, like the title says, a charctercontroller script and tried to add a crouching ability. So this is the code I came up with (I erased the unnecessary code):

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float movementSpeed = 5.0f;
 
     public float rotationSpeed = 5.0f;
     float verticalRotation = 0;
     float rotationBoundary = 60.0f;
 
     public float jumpSpeed = 0.0f;
     float verticalVelocity = 0;
 
     float ccSizeCrouch = 2;
     int crouchOffset = 1;
     float cameraCrouchOffset = - 0.75f;
 
     //Methoden Deklaration
 
 
 
 
 
     // Use this for initialization
     void Start () 
     {
         Screen.lockCursor = true;
 
 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         //Kamera Drehung zum Umschauen
         
         float lookHorizontal = Input.GetAxis ("Mouse X") ;
         transform.Rotate (0, lookHorizontal, 0);
         
         verticalRotation -= Input.GetAxis ("Mouse Y") ;
         verticalRotation = Mathf.Clamp (verticalRotation, -rotationBoundary, rotationBoundary);
         
         
         //Speichert die Kamera Rotation als Quaterion Euler um sie mit Vector 3 verwenden zu können
         Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
 
 
 
         //Hier wird die Bewegung eingerichtet
         CharacterController cc = GetComponent<CharacterController>();
 
         //Ermöglicht dem Char zu springen
 
         if (Input.GetButtonDown ("Jump") && cc.isGrounded) 
         {
             verticalVelocity = jumpSpeed;
         
         }
 
         //Schwerkraft
 
         verticalVelocity += Physics.gravity.y * Time.deltaTime;
 
         //Grundlegende Bewegung auf den Achsen
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal, verticalVelocity, moveVertical);
 
         //Sprinten
 
         if (Input.GetButton ("Run")) 
         {
             movementSpeed = 10.0f;
         } 
         else 
             movementSpeed = 5.0f;
 
         //Setzt den neuen Transform in Abhänigkeit von Rotation
         movement = transform.localRotation * movement;
         cc.Move ((movement * movementSpeed)*Time.deltaTime) ;
 
         Implementrierung von Crouch
 
         if (Input.GetButton ("Fire3"))
         {
 
             CharacterController CrouchHeight = (cc.height/ccSizeCrouch) * Time.deltaTime ;
             cc.transform.localPosition.y -= crouchOffset * Time.deltaTime;
             Camera cameraCrouchposition = (camera.main.transform.localPosition.y - cameraCrouchOffset) * Time.deltaTime;
 
             if (Input.GetButtonUp ("Fire3"))
             {
                 CrouchHeight = (cc.height*ccSizeCrouch)*Time.deltaTime;
                 cc.transform.localPosition.y += cameraCrouchOffset * Time.deltaTime;
                 cameraCrouchposition = (camera.main.transform.localPosition.y + cameraCrouchOffset) * Time.deltaTime;
             }
         }
 
 
 
 
 
         }
 
 }
 


After I saved this one and switched back to Unity, I got some errors, which I wasn't able to get fixed.

alt text

I would appreciate your help and your opinion with that. And please excuse my spelling and language skills, I'm not a native speaker and it has passed some time since the last time I wrote english.

unbenannt.jpg (95.8 kB)
Comment
Add comment · Show 1
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 Jackson21 · Mar 03, 2015 at 09:49 AM 0
Share

So as suggested I posted the whole script.

1 Reply

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

Answer by maccabbe · Mar 03, 2015 at 01:16 PM

  1. At line 89 you cannot define a CharacterController as a float, CharacterController CrouchHeight = (cc.height/ccSizeCrouch) * Time.deltaTime ; Since CrouchHeight's value is not used anywhere else I'm not really sure what you were trying to do here.

  2. At line 90 you attempt to modify the y coordinate of transform.position but transform.position uses get/set so you need to assign the whole thing. You probably want

    cc.transform.localPosition -= new Vector3(0, crouchOffset * Time.deltaTime, 0);

  3. At line 91 you cannot use camera.main as Camera.main is a static variable and must be gotten using Camera.main (C# is case sensitive). In addition, at line 91 you then try to assign a float to a variable of type camera. You might be trying to do the following

    Camera.main.transform.localPosition -= new Vector3(0, cameraCrouchOffset* Time.deltaTime, 0);

  4. The other 4 errors are the same as 1-3 but on lines 95-97 (inside the if statement).

Comment
Add comment · Show 1 · 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 Jackson21 · Mar 03, 2015 at 07:51 PM 0
Share

Thank you very much for your fast and accurate help. With your suggestions the errors were fixed. Despite, I don't get that script to do what it shall do. Think my approch at point one of your solution was kind of that problem. Tried to reduce the size of the CharacterController Collider, by dividing the size through two.

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

1 Person is following this question.

avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

[C#]CharacterController Turning 2 Answers

Networked player movement very choppy, rotation works fine - UNET 1 Answer

How to hold objects in third person? 1 Answer

Change Direction[C#] 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