I want to add some Velocity controll to my CharacterController
Below I will post my Playercontroller script. In there I have a velocity variable that will controll the players movement.
I want to be able to make plattforms and other such things that will make my player fly up in the air when they interact and such.
To test the mechanics I am trying to add velocity when pressing jump, however; this is not taking effect.
The reason is because the velocity is reset to my "Horizontal / Vertical" * movementSpeed. How would I best go about on fixing this?
Sorry if I am unclear; I have a terrible headache and my main language is Swedish.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
// Public variables
public float movementSpeed = 10.0f;
public float jumpSpeed = 10.0f;
public float mouseSensitivity = 1.0f;
public float maxHeadTilt = 75.0f;
// Private Variables
private Vector3 velocity = new Vector3(0,0,0);
private float verticalRotation = 0.0f;
private Vector3 movement = new Vector3(0,0,0);
// Private Objects
private CharacterController cc = null;
private Camera _camera = null;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController> ();
_camera = GetComponentInChildren<Camera> ();
}
// Update is called once per frame
void Update () {
// Velocity
velocity.x = Input.GetAxis ("Horizontal") * movementSpeed;
velocity.y += Physics.gravity.y * Time.deltaTime;
velocity.z = Input.GetAxis ("Vertical") * movementSpeed;
// Inputs
if (Input.GetButtonDown ("Jump"))
{
velocity.z = jumpSpeed;
velocity.y = jumpSpeed;
}
// MouseLook
transform.eulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * mouseSensitivity, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -maxHeadTilt, maxHeadTilt);
_camera.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
// Movement
movement = new Vector3 (velocity.x, velocity.y, velocity.z);
movement = transform.rotation * movement;
cc.Move(movement * Time.deltaTime);
Debug.Log (velocity);
}
}
Do you want your character to be pushed both upwards and forwards when you jump? If not, you don't need
velocity.z = jumpSpeed;
I am using it to test. I am trying to make the character move forward, but only the .y value is being read. I am trying to make the character fly forward, but it is reset from the code above, making the velocity.z = Input.GetAxis ("Vertical") * movementSpeed.To test the mechanics I am trying to add velocity when pressing jump, however; this is not > taking effect.
basically, so far I can only modify my Y velocity.
Answer by Konomira · Oct 11, 2015 at 03:48 PM
Your moveSpeed is the same as your jumpSpeed, you wouldn't notice any difference.
if you were to have
velocity.z += jumpSpeed;
instead of
velocity.z = jumpSpeed;
then it should work.
Didn't work either.
http://i.imgur.com/25RnPFQ.png
This is the debug of my velocity. As you can see, it is being reset to 0 directly after pressing space. But that only applies to X and Z, not Y.
velocity.x = Input.GetAxis ("Horizontal") * movementSpeed;
velocity.y += Physics.gravity.y * Time.deltaTime;
velocity.z = Input.GetAxis ("Vertical") * movementSpeed;
these are the lines that "ruins" it. But I don't know how to fix all of this.
Try this:
if(Input.GetButtonDown("Jump"))
{
velocity.z = (Input.GetAxis ("Vertical") * movementSpeed) + jumpSpeed;
velocity.y = jumpSpeed;
}
Didn't quite work. Something that does give me the "desired effect" was this:
I added a new float, made the velocity automatically add the force into it's calculations at all times; then I just changed the float.
I can probably make an automated system that will strive to make my float equals to 0.
I appreciate your help! :D