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 /
This question was closed Feb 10, 2014 at 07:28 PM by Benproductions1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Infinite_Gamer · Jan 25, 2014 at 08:14 PM · c#movementbeginner

[Answered]Rigid body Movement C#

Sorry for all this stuff about movement. I am trying to make a rigid body based player movement.I found this script off of the unify wiki. But I am trying to make a sprinting ability to it. I have been goggling and have found no answer that have fixed the problem. Here is the script that I have been working on.

Edited script

     using UnityEngine;
 using System.Collections;
 
 
 //This script has a problem where you get stuck on walls
 
 [RequireComponent (typeof (Rigidbody))]
 [RequireComponent (typeof (CapsuleCollider))]
 
 public class PlayerMovement : MonoBehaviour 
 {
 
     public float PlayerSpeedWalk = 5.5f;
     public float PlayerSpeedSprint = 15.0f;
 
     public float gravity = 10.0f;
     public float jumpHeight = 2.0f;
 
     public float maxVelocityChange = 15.0f;
 
     public bool IsJump = true;
     public bool IsCrouch = false;
     public bool IsSprint = true;
     private bool grounded = false;
 
     public Vector3 targetVelocity = Vector3.zero;
     
     
     
     void Awake () 
     {
         rigidbody.freezeRotation = true;
         rigidbody.useGravity = false;
     }
     
     void FixedUpdate () 
     {
         if (grounded) 
         {
             // Calculate how fast we should be moving
             Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             targetVelocity = transform.TransformDirection(targetVelocity);
             targetVelocity *= PlayerSpeedWalk;
             
             // Apply a force that attempts to reach our target velocity
             Vector3 velocity = rigidbody.velocity;
             Vector3 velocityChange = (targetVelocity - velocity);
             velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
             velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
             velocityChange.y = 0;
             rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
             
             // Jump Ability
             if (Input.GetButton("Jump") && IsJump) 
             {
                 rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
             }
 
 
             //Sprint Ability
 
             //Sprint Bool
 
 
             //Sprint Movement
             if(Input.GetKey(KeyCode.LeftShift))
             {
                 // Calculate how fast we should be moving
                 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                 targetVelocity = transform.TransformDirection(targetVelocity);
                 targetVelocity *= PlayerSpeedSprint;
 
                 // Apply a force that attempts to reach our target velocity
                 velocity = rigidbody.velocity;
                 velocityChange = (targetVelocity - velocity);
                 velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
                 velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
                 velocityChange.y = 0;
                 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
             }
 
             //Crawl Ability
         
 
         }
         
         // We apply gravity manually for more tuning control
         rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
         
         grounded = false;
     }
     
     void OnCollisionStay () 
     {
         grounded = true;    
     }
 
     //Calculates the jump height
     float CalculateJumpVerticalSpeed() 
     {
         // From the jump height and gravity we deduce the upwards speed 
         // for the character to reach at the apex.
         return Mathf.Sqrt(2 * jumpHeight * gravity);
     }
 }

This script comes up with an error that says Assets/Scripts/Player Scripts/PlayerMovement.cs(69,41): error CS0136: A local variable named targetVelocity' cannot be declared in this scope because it would give a different meaning to targetVelocity', which is already used in a `parent' scope to denote something else

I have no clue on how to fix this problem. Any help/advice would be helpful

thanks in advanced

Infinite Gamer

Comment
Add comment · Show 6
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 getyour411 · Jan 25, 2014 at 08:17 PM 0
Share

Your line #s are off because of the way you formatted the code, but you have targetVelocity declared at the top and then redefined in the middle with the "Vector3" keyword - I'd try removing that and see if it works

avatar image Benproductions1 · Jan 25, 2014 at 11:46 PM 0
Share

Please format your code, if you don't know how, watch the tutorial video on the right and read the FAQ.

avatar image Infinite_Gamer · Jan 26, 2014 at 03:04 AM 0
Share

Getyour411 that is how it was before and it does not work like that either. Ok I will reformat my code

avatar image Infinite_Gamer · Jan 27, 2014 at 12:45 AM 0
Share

Ok,I figured out what it was.but there is still a problem. When I try to sprint it does not change the speed of the player.

avatar image Infinite_Gamer · Jan 27, 2014 at 06:58 PM 0
Share

I get no errors but the player always stays the same speed even if you put the speed to really slow like .5.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

20 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

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

Velocity limiter not functional 1 Answer

Help on Player Movement Script C# 1 Answer

Where can I find tutorials for scripting in 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