Creating own "x" and "z" axis to move player along?
Hey, horrible beginner here, I had the following plan:
A plane-object (big, transparents cube in this picture) is always setting its transform.position to that of the player (white cylinder). When pushing the right control-stick on the controller left and right, the plane-object rotates (around the player) - which is what I want, because the Main Camera is attached to the plane-object, so it effectively hovers around the player as well. Also attached to the plane-object are four colored squares, the blue one declaring the "front." The front-cube is always facing the camera.
My problem now is that I want to move the player forward, backward, left and right not according to the World Axis, but two Axis established by the colored blocks. So there should be an artificial Z-Axis between the blue and yellow cube, and an artificial X-Axis between the red and green cube, and when I, for example, press "Down", I want the player-object to move towards the blue cube, "along" the Axis between the blue and the yellow cube. Does that make sense?
Long story short, I want to create a simply camera-system where the player's input and the resulting direction of the movement/velocity is relative to the camera-angle, and I thought this could be a good approach if only I can somehow establish that the player should be moving along those two new imaginary axis. Ideally, I would derive the direction that the player-object should be facing from this system as well.
Is that feasible and possible? If so, how? Or is it needlessly complicated, and I'm forgetting something?
Thanks in advance!
Not quite sure I understand... but do you mean that, you want the player's moving direction based on the camera's view? If so than this is doable, but it requires some scripting. If you are very new to scripting, I would recommend you to find in Asset Store or UnityWiki
Oh, haha, I was definitely implying that I wanted to solve this problem by scripting. I'm still somewhat unexperienced, but not entirely unfamiliar, and yes, you're correct! I want the player to move according to how the camera is rotated. I've since asked somebody else - apparently, a more reasonable solution would be to check for the camera's relative position in world-space and realign the characters' axes to that. I don't need these boxes as an indicator for axes anymore.
Thank you for your advice though! Really nice.
Oh ok than it shall be fine :D. I'm also not that experienced so I'll just skip explaining details... Here's a quick code I created if you need something. And maybe you can look at this post or google "Unity 3rd Person Controller", there should be a lot of tutorials!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Put this on a object. In play mode press WASD and it should be moving based on camera angle.
public class Foo : $$anonymous$$onoBehaviour {
public Camera camera;
public float translationSpeed = 5;
public float rotationSpeed = 10;
private float inputAxisHorizontal;
private float inputAxisVertical;
private Vector3 desiredTranslation;
private Quaternion desiredRotation;
// Use this for initialization
void Start () {
if (!camera) {
camera = Camera.main;
}
}
// Update is called once per frame
void Update () {
inputAxisHorizontal = Input.GetAxis("Horizontal");
inputAxisVertical = Input.GetAxis("Vertical");
desiredTranslation = new Vector3(inputAxisHorizontal, 0, inputAxisVertical) * Time.deltaTime * translationSpeed;
desiredTranslation = camera.transform.TransformDirection(desiredTranslation);
desiredRotation = Quaternion.LookRotation(
Vector3.RotateTowards(transform.forward, desiredTranslation, Time.deltaTime * rotationSpeed, 0.0F));
transform.Translate(desiredTranslation, Space.World);
transform.rotation = desiredRotation;
}
}