- Home /
The question is answered, right answer was accepted
[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
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
Please format your code, if you don't know how, watch the tutorial video on the right and read the FAQ.
Getyour411 that is how it was before and it does not work like that either. Ok I will reformat my code
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.
I get no errors but the player always stays the same speed even if you put the speed to really slow like .5.
Follow this Question
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