SebLague / Spherical-Gravity

MIT License
156 stars 53 forks source link

Backward vector of Camera Look Vector #3

Open atmasol opened 6 years ago

atmasol commented 6 years ago

Hi Sebastian

Thanks for the useful tut on spherical gravity. I have a small feature request if you are able to fit it in. I am a newbie to Unity3d but have some experience in coding. How do you get the backward vector of where the camera is pointing irrespective of the player body?

The idea is to make a thrust gun that when triggered pushes the player body in the opposite direction of where the camera is pointing. Would be cool if it applies to your spherical gravity tut.

Regards

Lee

atmasol commented 6 years ago

Here is a simple kickback script to get the idea going but it has some limitations. It is very abrupt and does not make use of fixedDeltaTime.

Below this the original jetpack script that was used for reference.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class KickBack : MonoBehaviour {

    public float speed = 3;

    public CharacterController CharCont;
    //public FirstPersonController fPC1;

    //public Vector3 currentVector = Vector3.up;

    //public Vector3 cameraVector = Vector3.forward;
    public Vector3 cameraVector = Camera.main.transform.forward;

        //The orthogonal basis vector right and up could be found in the same way:

    //Camera.main.transform.up;
    //Camera.main.transform.right;

    public float CameraForce = 10;

    public float MaxForce = 2;

        // Update is called once per frame
    void FixedUpdate () 
    {

        if (Input.GetButtonDown("Fire1"))
        {

            //CharCont.Move((cameraVector * speed * Time.fixedDeltaTime - CharCont.velocity * Time.fixedDeltaTime) * CameraForce);
            //CharCont.Move((cameraVector * speed - CharCont.velocity) * CameraForce);

            transform.position -= this.transform.forward * CameraForce * Time.deltaTime;
            //transform.position += this.transform.up * CameraForce * Time.fixedDeltaTime;

            //CharCont.Move(this.transform.up * CameraForce * Time.fixedDeltaTime);
        }

    }

}

Hack from Gamads tut on youtube here https://www.youtube.com/watch?v=2merbiVLv28

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class JetPack : MonoBehaviour {

    public float speed = 3;

    public CharacterController CharCont;
    public FirstPersonController fPC;

    public Vector3 currentVector = Vector3.up;

    //public Vector3 cameraForwardVector = Vector3.up;

    public float CurrentForce = 0;

    public float MaxForce = 5;

        // Update is called once per frame
    void FixedUpdate () 
    {

        if(Input.GetKey(KeyCode.Space) && MaxForce > 0)
        {

            MaxForce -= Time.deltaTime;

            if (CurrentForce < 1)
                CurrentForce += Time.deltaTime * 10;
            else
                CurrentForce = 1;
        }

        if(MaxForce < 0 && CurrentForce > 0)
        {

            CurrentForce -= Time.deltaTime;

        }

        if (!Input.GetKey(KeyCode.Space))
        {

            if (CurrentForce > 0) 
                CurrentForce -= Time.deltaTime;
            else
                CurrentForce = 0;

            if (MaxForce < 5)
                MaxForce += Time.deltaTime;
            else
                MaxForce = 5;

        }

        if (CurrentForce > 0)
            UseJetPack();

    }

    public void UseJetPack()
    {

        //if (fPC.m_Jump) 
            //fPC.m_Jump = false; 

        currentVector = Vector3.up;

        currentVector += transform.right * Input.GetAxis("Horizontal");
        currentVector += transform.forward * Input.GetAxis("Vertical");

        CharCont.Move((currentVector * speed * Time.fixedDeltaTime - CharCont.velocity * Time.fixedDeltaTime) * CurrentForce);
    }
}