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
1
Question by Pixel-Sword · Apr 13, 2015 at 07:42 PM · c#javascriptscripting problem

Character Clipping Through Walls When Uncrouching and Receiving Errors

I have been attempting to fix one of my scripts to make sure that my character does not clip through walls when the player unchrouches, or in this scenario, lets go of "C." I tried adding a Raycast that I turned from Javascript into C#, but, this is giving me quite a few errors. These errors are:

  1. Assets/Scripts/Crouching/RunAndCrouch.cs(50,68): error CS0165: Use of unassigned local variable hit' 2. Assets/Scripts/Crouching/RunAndCrouch.cs(50,36): error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit, float)' has some invalid arguments

  2. Assets/Scripts/Crouching/RunAndCrouch.cs(50,36): error CS1620: Argument #3' is missing out' modifier

I am attempting to make the script send a test message "Hit" into the console when an object is above the character, but the script returns these errors and prevents me from playing the scene. Here is the script:

using UnityEngine; using System.Collections;

public class RunAndCrouch : MonoBehaviour { public float walkSpeed = 7; // regular speed public float crchSpeed = 3; // crouching speed public float runSpeed = 20; // run speed [SerializeField] private float _standHeight = 0.5f; [SerializeField] private float _radius = 0.5f; [SerializeField] private LayerMask _terrianLayers;

 private CharacterMotor chMotor;
 private Transform tr;
 private float dist; // distance to ground
 private bool CanStand()
 {
     return Physics.CheckCapsule(transform.position, transform.position + new Vector3(0f, _standHeight, 0f), _radius,
                                 _terrianLayers);
 }
 
 // Use this for initialization
 void Start () 
 {
     chMotor =  GetComponent<CharacterMotor>();
     tr = transform;
     CharacterController ch = GetComponent<CharacterController>();
     dist = ch.height/2; // calculate distance to ground
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
     float vScale = 1.0f;
     float speed = walkSpeed;
     
     if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
     {
         speed = runSpeed;            
     }
     
     if (Input.GetKey("c"))
     { // press C to crouch
         vScale = _standHeight;
         speed = crchSpeed; // slow down when crouching
         var up= transform.TransformDirection(Vector3.up);
         RaycastHit hit;    
         Debug.DrawRay(transform.position, up * 1.1f, Color.green);
         
         if(Physics.Raycast(transform.position, up, hit, 1.1f)){
             Debug.Log("Hit");   
             }
     }

     chMotor.movement.maxForwardSpeed = speed; // set max speed
     float ultScale = tr.localScale.y; // crouch/stand up smoothly 
     
     Vector3 tmpScale = tr.localScale;
     Vector3 tmpPosition = tr.position;
     
     tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
     tr.localScale = tmpScale;
     
     tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position        
     tr.position = tmpPosition;
 }
 }

I am not sure how I would go about fixing these errors or if there is a different way to go about fixing my character clipping through things when it crouches. Can anyone possibly help me fix these errors or find another solution?

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 Eno-Khaon · Apr 14, 2015 at 09:21 AM 1
Share

First off, you need to tweak your Raycast line:

 if(Physics.Raycast(transform.position, up, hit, 1.1f))

should read

 if(Physics.Raycast(transform.position, up, out hit, 1.1f))

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Paul-Sinnett · Apr 14, 2015 at 09:23 AM

On line 38 add the keyword "out"


if(Physics.Raycast(transform.position, up, out hit, 1.1f)){
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 Pixel-Sword · Apr 14, 2015 at 05:27 PM

Okay, thank you both. I managed to fix the errors, and I have figured out how to stop the character from clipping through walls.

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 Paul-Sinnett · Apr 14, 2015 at 05:41 PM 1
Share

Cool. Don't forget to mark this question as answered, so that it no longer appears in the unanswered questions list.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet bounce issue using Vector3.reflect 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Restricting movement with Mathf.Clamp 4 Answers

Script will be found on the client but not on the server. 0 Answers


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