Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward vector of Camera Look Vector #3

Open
atmasol opened this issue Nov 27, 2017 · 1 comment
Open

Backward vector of Camera Look Vector #3

atmasol opened this issue Nov 27, 2017 · 1 comment

Comments

@atmasol
Copy link

atmasol commented Nov 27, 2017

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
Copy link
Author

atmasol commented Nov 27, 2017

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);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant