- Home /
Object falling through terrain with script
Hi! First i want to sorry for my english, it's not my native language.
I have this little script that resizes object ( the player ) from 1.0 to 0.5 on the axis Y.
using UnityEngine;
using System.Collections;
public class Dodge : MonoBehaviour {
public bool isDodged = false;
// Use this for initialization
void Start () {
isDodged = false;
}
// Update is called once per frame
void Update () {
if ( Input.GetKeyDown(KeyCode.LeftControl)) isDodged = true;
if ( isDodged == true ) {
Vector3 scale = transform.localScale;
scale.y = 0.5f; // This is where it decreases to 0.5
transform.localScale = scale;
}
if ( Input.GetKeyUp (KeyCode.LeftControl)) isDodged = false;
if ( isDodged == false ) {
Vector3 originalsize = transform.localScale;
originalsize.y = 1f;
transform.localScale = originalsize;
}
}
}
I start with original 1.0 value, when i hold down LeftCTRL the player decreases to 0.5, and after i release the button, it goes back to 1.0 Yes, this works, but when it goes back to size 1.0, it falls through the map. I think its because it doesn't has time to calculate colission with map, OR it begins to resize from the center, so a little part remains under the terrain. How could i fix that?
Thank you!
Hi, if you are struggling with crouch, check out this post. http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html
Answer by mirkobon · Sep 02, 2013 at 03:10 PM
Add this to your script when it resizes:
transform.position.y += 1;
maybe I'm wrong.
It's not a bad idea, but it doesn't looks good when playing in 3rd person :/
Didn't know it was 3rd person try make an animation than?
Answer by save · Sep 02, 2013 at 03:29 PM
You could Raycast downwards to get the position where to place the character right after rise. You would want to shoot it a bit above the player towards -Vector3.up. Then place the character with an offset of 50% of body height (.5f in your case?), if pivot is at center. Think in terms of where the collider is ending up from crouching, rising and standing, it's usually good to monitor it with the Scene view while playing.
Your answer
Follow this Question
Related Questions
My character will fall through the floor with this script:... 1 Answer
How can I make a sound deploy after colliding with a certain object (or terrain)? 1 Answer
When my prefabs are instantiated they don't have their script attached 1 Answer
Can't use tree brush tool 0 Answers
Keep an object on the gound. 2 Answers