#pragma strict
//this function hold all the controller for the main character
private var canMove:boolean;
function Start ()
{
canMove = true;
}
function Update ()
{
if(canMove == true)
{
// character control for PC
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
gameObject.transform.position.z +=5*Time.deltaTime;
//gameObject.transform.position.y += 0.1;
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
gameObject.transform.position.x -=5*Time.deltaTime;
}
if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
gameObject.transform.position.z -=5*Time.deltaTime;
}
if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
gameObject.transform.position.x +=5*Time.deltaTime;
}
}
}
hi, im having problem with my character movement
i gave my character a rigidbody and box collider , but somehow everytime my character moves he shakes … i thought it was because of the collider thingy
so i gave him some additional script to lift him up little bit from the land :
gameObject.transform.position.y += 0.1;
well, it didnt fix my problem…
im new at using unity please help…thankyou
Hmm, your character has a rigidbody attached to it right? I assume it’s using gravity then, and that it’s kept at the surface by its collider.
When you’re dealing with a rigidbody object, you should always work in FixedUpdate, instead of update. FIxedUpdate will run once on each physics step, while update only runs once per frame. Most shaking issues come from using Update instead of FixedUpdate.
Also, a few tips, you don’t need to use gameObject.transform.position to get to the position. Every MonoBehaviour script already keeps a reference to the transform component, so just transform.position will do there.
Also, you might want to make that ‘5’ into a public ‘walkSpeed’ variable. Makes tweaking things easier, since unity will expose public variables in scripts to the inspector panel, so you can tweak without having to modify the code (not to mention tweaking the same value in several different places)
Oh, and needless to say, you don’t need that ‘character elevation’ script. That’s not fixing the problem, it’s sweeping it under the rug.
Well, hope this helps. Good luck!
Cheers
You are using a rigidbody with a collider and using math to move it. Let the physics engine handle it.
Here is a physics Character Controller I wrote for a sub project (it is in C#):
using UnityEngine;
using System.Collections;
public class PhysicsCharacter : MonoBehaviour {
// generic controls
float speed = 8;
float jumpSpeed = 12;
// character controller
CharacterController controller;
public bool grounded = false;
// camera control
float x=0;
float y=0;
// Use this for initialization
void Start () {
if(!rigidbody)
gameObject.AddComponent<Rigidbody>();
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
rigidbody.useGravity = false;
Vector3 angles = Camera.main.transform.eulerAngles;
x = angles.y;
y = angles.x;
}
// Update is called once per frame
void Update () {
Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
input = transform.TransformDirection(input);
if(input.magnitude > 1) input.Normalize();
float gravity = Physics.gravity.y * 3;
Vector3 move = rigidbody.velocity;
Vector3 p1 = transform.position + Vector3.down * 0.5f;
float r = 0.45f;
RaycastHit hit;
grounded = false;
if(Physics.CapsuleCast(p1, p1, r, Vector3.down, out hit, 0.1f)){
if(Vector3.Dot(hit.normal, Vector3.up) > 0.5f) grounded = true;
}
if(grounded){
move = input * speed;
move.y = gravity * Time.deltaTime;
if(Input.GetButtonDown("Jump")){
move.y = jumpSpeed;
grounded = false;
}
} else {
move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
}
transform.rotation = Camera.main.transform.rotation;
Vector3 euler = transform.eulerAngles;
euler.x = 0;
transform.eulerAngles = euler;
rigidbody.velocity = move;// * Time.deltaTime
//CollisionFlags flags = controller.Move(move * Time.deltaTime);
//grounded = (flags CollisionFlags.Below) > 0;
}
void LateUpdate(){
// quick mouse orbit
x += Input.GetAxis("Mouse X") * 5;
y -= Input.GetAxis("Mouse Y") * 5;
if(y > 180) y -= 360;
if(y < -180) y += 360;
y = Mathf.Clamp(y, -60,60);
Camera.main.transform.rotation = Quaternion.Euler(y,x,0);
Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position;
}
}
Interesting code there big. Been meaning to write a decent physics based char controller. might have a play with yours.
Although I think it should be done in fixedupdate as its physics related.
it may be sounds noob but how to use fixed update … i checked unity reference but i dont quite get it …
someone can give me example plss if you dont mind …
also my surface will not be totally flat , and i want my character follow that surface as well when moving …thats why i want to use riggidbody
and thx BigMisterB for the script i think it works …but my character can’t move …
Where you normally have function Update, to use a fixed update simply use
function FixedUpdate
so
function FixedUpdate () {
//JS
}
//or...
void FixedUpdate () {
//CS
}