I added a sprinting script to my character today, and it works, but now when I am sprinting my character pushes through walls and objects and just goes right through them.
The objects have colliders, any objects with mesh colliders I have clicked “is convex”, and I have toyed with adding rigidbodies to the walls and turning off gravity and clicking “is kinematic”. I have messed with all of these options but alas anytime my character sprints over a certain speed, it goes through.
Here is my character movement script:
usingUnityEngine;
usingSystem.Collections;
public class topDownMovement : MonoBehaviour
{
//ignore this, it is just for a sound when I pick up items
public AudioClip collect;
//these let me adjust the movement and turn speed of my character
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update()
//sets the input; "Strafe" is just a custom axis I set up in the input manager
{
float strafe = Input.GetAxis ("Strafe") * speed;
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
strafe *= Time.deltaTime;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (strafe, 0, 0);
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}
void OnTriggerEnter(Collider other)
//ignore the next part as well, it is just for the sound on picking up an item
{
if (other.gameObject.CompareTag("Pick Up"))
{
GetComponent<AudioSource>().PlayOneShot(collect);
other.gameObject.SetActive (false);
}
}
}
And… Here is the sprinting script:
usingUnityEngine;
usingSystem.Collections;
public class Sprint : MonoBehaviour
{
topDownMovement tdm;
private float walkSpeed;
private float sprintSpeed;
void Start ()
//gets the topDownMovement script from above for reference, defines walk as the speed on that script, and defines sprint as that speed multiplied by a value.
{
tdm = GetComponent<topDownMovement> ();
walkSpeed = tdm.speed;
sprintSpeed = walkSpeed * 3;
}
void Update ()
//just defines that pressing or lifting the left shift key will engage or disengage the sprint state.
{
if(Input.GetKeyDown (KeyCode.LeftShift))
tdm.speed = sprintSpeed;
if(Input.GetKeyUp (KeyCode.LeftShift))
tdm.speed = walkSpeed;
}
}
I am very new to this but I am not entirely convinced that anything is wrong with the code, per se; if anyone could help me figure out what is causing the character to pass through walls like a ninja ghost it would make my day.
Your character is a rigidbody? If so than you should be using addforce and not directly translating. You also should be using fixedupdate. After doing all this, you can enable continuous dynamic collision detection on your rigidbody and all should be good.
Ok here is what i’ve come up with so far but the console is kicking a few errors back at me. I apologize if the code is pretty bad; I’ve only been learning for about a week now. I am being told that that void cannot be used in this context; that there is a parsing error, and an unexpected symbol ‘(’
Yea, it seems you are missing the } for your Update as Korno has said. You also have an extra one at the bottom. You will also need to declare “float strafe” and “float translation” on top so that FixedUpdate can access it.
Also, I hope that the indentation your scripts have here are only bad because of some copy paste issue when bringing your script onto the forums. If not and this is how you indent your code, then I highly advise you to make your code cleaner. It can also help prevent problems with missing or having extra closing brackets.
Here is the fixed code (or should be fixed, didn’t test it)
Click for code
Korno yeah that closing } fixed those three errors, but as you and HiddenMonk have pointed out, other issues. I am going to try these suggestions here after breakfast and i’ll let you guys know what happens.
As for my indentation, yeah I’m not sure why but when I copy/paste i find that a lot of spaces disappear and that I have to try to fix it by hand, but it doesn’t show up quite how i’m trying to edit it. Not sure if that has something to do with using a mac or not, but i’ll keep playing with it until i get it right.
Thanks again and i’ll update after i get a chance to try this.
when you paste your code into the code snippet thing (17th icon from the left in the text box), it will look weird. Just leave it and when you post it should be just as you copied it.
Ok so I tried this code snippet and all errors are in fact gone, however the character object does not actually move. This is my first time trying to use AddForce; will the “speed” float I have above actually do anything now or is there a different way that I need to be defining how AddForce affects the object?
Player object is not Kinematic and positions are not constrained, although rotations are. Turning off the rotations the player still does not move (and falls over).
Oh my. Now I’m getting a bit overwhelmed. I may have to almost go back to the drawing board here. Using ForceMode.VelocityChange does in fact now move the character, but strictly along the world space, rather than relative to the player rotation.
The player also moves and keeps moving now that it is using physics, so when you keyup, she continues moving. I’m guessing maybe this can be fixed by increasing drag, but I am assuming that there is a better way.
I can’t tell you how much I appreciate all of the time people here are taking to help me learn this. I’m going to go read up on ForceMode and see if I can teach my brain a few things.
doing “ply.velocity = Vector3.zero;” should slow things down. I do not know what doing this may do to collision detection though, it is usually advised to not directly change the velocity, but doing it this way is easy.
So this works, and I am able to just adjust the speed field to get the character to move at the speed that I want. Works with the sprint script and clipping seems to be completely eliminated. This has cause one new issue, which I’m quickly learning is the name of the game. Now when the character is not standing on a surface, she falls at an incredibly slow rate. I am guessing this has something to do with ply.velocity = Vector3.zero;
Would there be some way to write this as ply.velocity = Vector3(0,x,0); Where the “x” could be some sort of definition that allows gravity to still work correctly on the character? Am I sniffing up the right tree?
This absolutely does the trick. Clipping is fixed, I can adjust the character speed to make it feel right, gravity works.
So, just to be clear, ply.velocity.y, in this case, is basically setting the y axis velocity to the default while we set the other two to 0? Seems to be how it is functioning, I just want to make sure i’m understaning that in the correct way.
Also, thank you for the link. I am looking at it now and I like how in the context, they also make an adjustable field to be able to adjust gravity to make it easier to tweak. I’m probably going to try to add that little bit into my script as well since it seems like a good idea.
HiddenMonk - Thank you. You have been so helpful and patient with someone who just started trying to do this and I just can’t tell you how encouraging it is. I was afraid a week ago that posting in a place like this might get me a lot of GTFO responses, but it seems the community is really driven to make these aspects of game design less esoteric and more accessible to all. I’m diggin’ it here.
ply.velocity.y is your rigidbodies current velocity in the y axis. Before, when we did ply.velocity = Vector3.zero; and your rigidbody was falling very slow, it was because any gravity that was applied to the y axis, we weren’t letting it build up and instead resetting it to 0. However, by taking the current y velocity and using that instead of a 0, we now let gravity do its thing without constantly resetting it. It can now keep building up until it hits a floor =).
So it isnt really the default, since 0 would probablly be concidered as the default.