Hi
Just a quick question about how you create a rigidbody controller, do you just create a rigidbody rename it player, add a camera and a collider of some sort?
Or is there more to it than that?
Thanks
Hi
Just a quick question about how you create a rigidbody controller, do you just create a rigidbody rename it player, add a camera and a collider of some sort?
Or is there more to it than that?
Thanks
Hey
Am I asking the wrong questions, nobody seems to want to answer my posts.
Anyway, I have been messing with this, have tried the Rigidbody and applied forces for movement, but this does not seem to work well enough for me, just doesnt feel right.
So, looks like its gonna be a Rigidbody with Kinematics, my previous question still stands and would like any tips you may be willing to share.
Thanks
Update …
So now I have a kinematic rigidbody with a capsule collider, and am moving it with transforms, I am also casting a ray to tell me when i,m in contact with an object, at which point i turn off gravity.
Problem now is, when i move I still fall through the terrain.
So I think what i need is another ray in the direction of movement( ideally this will turn for the current direction, so i only need one) which tells me how high i need to be to stop myself falling through.
Here,s my code …
using UnityEngine;
using System.Collections;
public class RigidBodyController : MonoBehaviour {
private Vector3 newPosition;
private float inputX;
private float inputZ;
private float mouseX;
private float gravity;
private bool grounded = false;
void Start ()
{
Screen.lockCursor = true;
gravity = Physics.gravity.y;
}
bool IsGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, 0.5f + 0.05f);
}
void FixedUpdate ()
{
inputX = Input.GetAxis("Horizontal");
inputZ = Input.GetAxis("Vertical");
mouseX = Input.GetAxis("Mouse X");
newPosition.x = inputX * 10f;
newPosition.z = inputZ * 10f;
transform.Rotate (0, mouseX * Time.deltaTime * 30, 0);
newPosition = transform.rotation * newPosition;
rigidbody.MovePosition(rigidbody.position + newPosition * Time.deltaTime);
if(!IsGrounded())
{
transform.Translate(0, gravity * Time.deltaTime, 0);
}
}
}
Its basic I know, any chance someone can give me some advice on this? Is my current approach workable?
Thanks
Hey
So, an update on my progress…
I made a simple script to control the default character controller which worked, the drawback being, you cant change the collider, and if you want to create a good game, your going need accurate hit detection, which the default capsule cant even begin to provide, and also the fact that no matter what I did, it would get stuck in certain situations.
So I started looking at ways in which you could do it, which led me to rigid bodies. They look promising as you can use any collider that you like. The drawback with these or so I thought is that you could only move it by adding force, which doesn’t feel realistic, but if you make it Kinematic, then you can use MovePosition commands, which is good, but then you lose the ability to get collision and trigger messages.
So the conclusion I have come up with is to use a non Kinematic Rigidbody, but set all the constraints to ticked. Doing this allows me to control the controller the way that feels good and still receive collision and trigger messages.
I hope this decision is not going to come a bite me on the backside down the road.
Once again, I ask for some advice from people that have been in my situation before, as now I have to find a way of keeping my controller above the terrain whilst moving, as it just falls through.
Thank You.
With raycasts you can get a RaycastHit point, wich would say you how high terrain is. With this information - move player to new correct position.
It’s only one of many many ways to make a character controller. Usually, rigidbody with forces used when you creating a spaceship or a car. Different way - modify rigidbody.velocity directly - so no applying focres to start moving, but still has forces and velocities to continue moving after releasing buttons.
Thanks for the reply.
So just for the purpose of following the terrain I am not really going to need collisions and triggers then? just multiple raycasts?
I also seem to be having an issue with gravity, I cant seem to apply it as an acceleration with Time,deltaTime, is this because its in the fixed update function?
Thanks again.
If you wish to write collision detection with terrain by yourself - yes.
But of course in that system you will need collider on terrain - to catch raycasts.
First of all, kinematic bodies doesn’t recieve any external forces, including gravity. If you adding your own gravity as Vector3 with negative y inside FixedUpdate - this should work.
Hey
Yeah I’m doing …
void Start ()
{
Screen.lockCursor = true;
gravity = Physics.gravity.y * Time.deltaTime;
}
and then …
newPosition.y = gravity;
and was wondering if I could use the following instead of raycasts to determine new height, of-course I would have to work out how to build the new position, but that’s something else …
height = Terrain.activeTerrain.SampleHeight(rigidbody.position);
Obviously change the rigidbody.position to the new position? should that work?
Thanks