- Home /
In-Air movement relative to camera
Hi!
I figured out how to make the player move while he's in the air.
I do it with this piece of code:
Vector3 airMove = new Vector3(moveInput.x * 6f, m_Rigidbody.velocity.y, moveInput.z * 6f);
m_Rigidbody.velocity = Vector3.Lerp(m_Rigidbody.velocity, airMove, Time.deltaTime * 2f);
Now how would I go about making it relative to the camera?
This question comes up a fair bit, be sure to search for your problem before asking a new question.
You will need to use the camera's orientation vectors to do this ( right
with X, up
with Y, forward
with Z).
I tried searching and didn't find the right answer that would help in my case. There's quite a few answers for doing the same thing but on ground. I want it in-air to control my jump direction after jumping.
Answer by RyanShackelford · Apr 30, 2018 at 05:40 PM
You could try attaching the camera directly to the player object. Also, standard assets might have a camera follow script in it.
The camera is already following my player. I want the movement to be relative to the camera so when I press W when I'm in the air i expect to go forward
Check out https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html
using UnityEngine;
using System.Collections;
// Add a thrust force to push an object in its current forward
// direction (to simulate a rocket motor, say).
public class ExampleClass : $$anonymous$$onoBehaviour
{
public float thrust;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddRelativeForce(Vector3.forward * thrust);
}
}
I tried using AddRelativeForce but couldn't get it to work either. I want to be able to change the direction of the jump mid-air
Import the Character's package in Unity, and look at the FPSController.prefab and the RigidbodyFPSController.prefab.
Assets > Import Package > Characters
I think you are going about movement in the air the wrong way. It should work the same way on the ground as in the air unless you have your code not adding force while in the air.
If that doesn't help with your script, I suggest looking for a character controller tutorial on YouTube or your favorite search engine.
Your answer
Follow this Question
Related Questions
How to relative movement system based on camera direction 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Character movement relative to the camera 1 Answer
Move player relative to camera 0 Answers
have 3rd person make a side jump -1 Answers