Hey guys,
I am trying to write a script for player movement and watched this youtube tutorial to learn how.
I tried to extend the functionality to move the player up and down on the y-axis to start simulating some form of flight. However, if I have the forward movement first in fixed update then it won’t register the flight variables and if I have the flight variables first then it doesn’t register forward and backward movement. Can anyone point me in the right direction or just tell me why this is happening
Here is the code I am working with right now
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
// initial speed for all movement
public float speed = 12;
// initial speed for all turning
public float turnSpeed = 100;
// replacing the jump function with this to achieve dashing
public float dashDist = 5.0f;
// delay input to make sure player wanted to make that action
public float inputDelay = 0.1f;
// rotation for the player
Quaternion targetRotation;
// cache the rigidbody that will be used to move object
public Rigidbody rb;
public float forwardInput, turnInput, fly;
// Use this for initialization
void Start () {
targetRotation = transform.rotation;
if (GetComponent ())
rb = GetComponent ();
else
Debug.LogError (“Need rigidbody”);
forwardInput = turnInput = 0;
}
void GetInput(){
forwardInput = Input.GetAxis (“Vertical”);
turnInput = Input.GetAxis (“Horizontal”);
fly = Input.GetAxis (“Fly”);
}
// Update is called once per frame
void Update () {
GetInput ();
Turn ();
}
// FixedUpdate called for physics based updates
void FixedUpdate(){
Move ();
Fly ();
}
void Move(){
Debug.Log (“in move method”);
if (Mathf.Abs (forwardInput) > inputDelay) {
//can move
rb.velocity = transform.forward * forwardInput * speed;
} else {
// no movement velocity = 0
rb.velocity = Vector3.zero;
}
}
void Turn(){
if (Mathf.Abs (turnInput) > inputDelay) {
//can turn
targetRotation *= Quaternion.AngleAxis (turnSpeed * turnInput * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
void Fly(){
if (Mathf.Abs (fly) > inputDelay) {
//can move
rb.velocity = transform.up * fly * speed;
} else {
// no movement velocity = 0
rb.velocity = Vector3.zero;
}
}
}
Thanks