This wont work with a Character Controller.
Here is the problem. this works with a humanoid character wearing a rigid body and cap.collider but that sucks balls when it comes to getting any sort of proper gravity movement and detection.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthControl : MonoBehaviour
{
public Image healthBar;
public float health;
public GameObject restartDialog;
void Start()
{
ShowRestartDialog(false);
}
void Update()
{
checkHealth();
}
void checkHealth()
{
healthBar.rectTransform.localScale = new Vector3(health / 100, healthBar.rectTransform.localScale.y, healthBar.rectTransform.localScale.z);
if (health <= 0.0f)
{
ShowRestartDialog(true);
}
}
void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("Death"))
{
SubtractHealth(25.0f);
}
else if (other.collider.CompareTag("Life"))
{
AddHealth(25.0f);
}
}
public void SubtractHealth(float amount)
{
if (health - amount < 0.0f)
{
health = 0.0f;
}
else
{
health -= amount;
}
}
public void AddHealth(float amount)
{
if (health + amount > 100.0f)
{
health = 100.0f;
}
else
{
health += amount;
}
}
public void ShowRestartDialog(bool c)
{
if (c)
{
Time.timeScale = 0.0f;
}
else
{
Time.timeScale = 1.0f;
}
restartDialog.SetActive(c);
}
public void Restart()
{
SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
}
}
I tried this adaptation to player movement but loose not only the ability to jump, but the health script ceases to work, but i get all my other proper movement and gravity.
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public Animator anim;
public CharacterController cc;
public float pushPower = 2.0F;
public float speed;
public float inputH = 20f;
public float inputV = 50f;
private bool run;
private bool jump;
private object hit;
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null)
return;
if (hit.moveDirection.y < -0.3F)
return;
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
cc = GetComponent<CharacterController>();
cc.detectCollisions = false;
run = false;
jump = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.RightControl))
{
run = true;
}
else
{
run = false;
}
if (Input.GetKey(KeyCode.Space))
{
jump = true;
}
else
{
jump = false;
}
{
CharacterController controller = GetComponent<CharacterController>();
Vector3 horizontalVelocity = controller.velocity;
horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
float horizontalSpeed = horizontalVelocity.magnitude;
float verticalSpeed = controller.velocity.y;
float overallSpeed = controller.velocity.magnitude;
if ((controller.collisionFlags & CollisionFlags.Above) != 0)
print("WTF");
}
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
anim.SetFloat("inputH", inputH);
anim.SetFloat("inputV", inputV);
anim.SetBool("run", run);
anim.SetBool("jump", jump);
float moveX = inputH * 20f * Time.deltaTime;
float moveZ = inputV * 50f * Time.deltaTime;
if (moveZ <= 0f)
{
moveX = 0f;
}
else if (run)
{
moveX *= 6f;
moveZ *= 6f;
}
}
}
so i went back to thios setup and i get my jumping and movement but of course i get stuck jumping in place but to keep my character from getting knocked off into space or falling over, you HAVE TO HAVE the rotations of x, y, and z checked, and if you uncheck Y on position, yes you can fall, but at a freaking snail pace, and it is frankly garbage.
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public Animator anim;
public Rigidbody Rb;
public float speed;
public float inputH = 20f;
public float inputV = 50f;
private bool run;
private bool jump;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
Rb = GetComponent<Rigidbody>();
run = false;
jump = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.RightControl))
{
run = true;
}
else
{
run = false;
}
if (Input.GetKey(KeyCode.Space))
{
jump = true;
}
else
{
jump = false;
}
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
anim.SetFloat("inputH", inputH);
anim.SetFloat("inputV", inputV);
anim.SetBool("run", run);
anim.SetBool("jump", jump);
float moveX = inputH * 20f * Time.deltaTime;
float moveZ = inputV * 50f * Time.deltaTime;
if (moveZ <= 0f)
{
moveX = 0f;
}
else if (run)
{
moveX *= 6f;
moveZ *= 6f;
}
Rb.velocity = new Vector3(moveX, 0f, moveZ * speed);
}
}
Please do not tell me "what number line" i need to put what where, because presently I am using visual studio and it isn't allowing me to use the "line number" views. If you know what line string to put in where that I am missing, simply specify copy these codes, and do the ///Here is the change/// so i can see what i was doing incorrectly.
Your answer
Follow this Question
Related Questions
Question on Physics.Simulate Behavior 0 Answers
Why is my trigger collider acting like a normal collider? 1 Answer
Car will only turn right (& ignored collders) 0 Answers
Block Issues 1 Answer
Colliders Don't Work no matter what 1 Answer