I’ve started on a project with a bunch of friends and we’re going to make a game together, using unity. I’m currently working on the movement but I got some problems. I want wasd to move the character, no rotation, and use the mouse to rotate. When I try to move with wasd, a and d works fine but when i go with w or s the character bounce away, it moves forward but still bounces very high.
Here the code:
using UnityEngine;
using System.Collections;
public class moveAround : MonoBehaviour {
public float speed = 4.0F;
public float jumpSpeed = 4.0F;
public float gravity = 20.0F;
private CharacterController controller;
private Vector3 moveDirection;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
if(!controller)
controller = gameObject.AddComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
if(controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
I like to create my own stuff, and since I’m a noob in scripting I think it’s a good way to learn. The inbuilt script that I’ve found didn’t workout with the controlls I want. I want to rotate with the mouse not with the a and d keys.
the CharacterMotor script doesn’t rotate with that, what you do is add the MouseLook script to the camera and the player, set the mouse look on the player to x only rotation, and set the one on the camera to y only rotation
Well, my problem still remains, the camera and rotation works like it should but I still can’t move forward or backwards without bounching way up in the air. I’ve tried several scripts, same problem with all of them.
Shouldn’t that be multiplied against time.deltaTime as well? (maybe it does from your last line, but then you double-multiple Time.deltaTime your gravity)
And:
moveDirection.y -= gravity * Time.deltaTime;
Won’t that keep subtracting even if it’s level with the ground?
‘moveDirection’ most likely represents a velocity, in which case it doesn’t need to be scaled by time.
With this type of movement system, it’s fairly common always to apply gravity and to let the collision detection/response system take care of any interactions with surfaces beneath the object.
This is a standard integration method. The first statement updates the velocity based on an acceleration, and the second statement updates the position based on the velocity. As such, it is in fact correct that both values are scaled by the time step.
There may be other problems with the code of course, but based on a quick look at least, the bits of code you refer to above all look correct to me.
It sounds like your character object has multiple rigidbodies. You need to make the main character collider NOT collide with its children - easiest way to do that is to create two layers, “Characters” and “Ragdolls” (or whatever makes sense to you), set the Character to be in the character layer and set its children to be in the Ragdolls layer, and then in Edit->Project Settings->Physics, set those layers to not collide with each other.