- Home /
Character is not grounding.
HELP!
Ive coded that :
using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class char_movement : MonoBehaviour
{
public float moveSpeed = 25.0f;
public float turnSpeed = 25.0f;
public bool gro = false;
public float grove = 8.0f;
public float velo = 1.0f;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
//sets grounded
gro = true;
print("Touched");
}
transform.position -= transform.TransformDirection(0,1.0f,0) * grove * velo * Time.deltaTime;
// If the char hits the ground
//If it's grounded
if(gro)
{
velo = 0.0f;
Movement();
}
if(!gro)
{
velo += 1.0f * Time.deltaTime;
}
}
void Movement()
{
if(Input.GetKey(KeyCode.W))
{
transform.position += transform.TransformDirection(0,0,1.0f) * moveSpeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S))
{
transform.position += -transform.TransformDirection(0,0,1.0f) * (moveSpeed-10.0f) * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up * -turnSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
}
}<code>
The character that ive made falls down as it needs to but it never collides the ground or reacts to the collisions below it.
Can anyone help me out?
Answer by DavidDebnar · Jul 17, 2011 at 03:47 PM
Place it anywhere.. Of you have a collider and your character moves downwards towards the ground, this will work :).
OnCollisionStay() { grounded = true; }
OnCollisionExit() { grounded = false; }
--David
It's in JavaScript, but I'm sure, that you can rewrite it into C#.
Answer by benk0913 · Jul 17, 2011 at 03:56 PM
David, It still doesn't works. Heres the new code
using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))] public class char_movement : MonoBehaviour {
public float moveSpeed = 25.0f; public float turnSpeed = 25.0f; public bool gro = false; public float grove = 8.0f; public float velo = 1.0f;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
transform.position -= transform.TransformDirection(0,1.0f,0) * grove * velo * Time.deltaTime;
// If the char hits the ground
//If it's grounded
if(gro)
{
velo = 0.0f;
Movement();
}
if(!gro)
{
velo += 1.0f * Time.deltaTime;
}
}
// Davids adding
void OnCollisionStay() { gro = true; print("Touched"); }
void OnCollisionExit() { gro= false; }
// __
void Movement() { if(Input.GetKey(KeyCode.W)) { transform.position += transform.TransformDirection(0,0,1.0f) moveSpeed Time.deltaTime; } if(Input.GetKey(KeyCode.S)) { transform.position += -transform.TransformDirection(0,0,1.0f) (moveSpeed-10.0f) Time.deltaTime; }
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up * -turnSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
}
}
Which part isn't working? I think it's the grounded one, but to be sure ;). So are you writing the script on your own, or just making it from some template? And is it using rigidbody or the character controller?
And please post your answers and comments, with the comments function, so it's not very messy, and there aren't 5 answers, because than the answerers may think, that it's already answered and the creator of the ask just forgot to close it, if you wan't answers, clean this up ;).
$$anonymous$$