- Home /
cant find a solution
hi i have been looking on the forums and answers for a while now and i cant find out how to fix the problem im having.
so im making a character move script and im moving it based on the camera rotation. this works fine, but i have my camera on an angle (45 degrees) and the guy always hits the ground when he goes forward and flys when i go back. this is my script:
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
public float WalkSpeed = 10.0f;
public float RunSpeed = 10.0f;
public float jumpHeight = 3.6f;
public float cooldown;
private bool Grounded = true;
public Transform camera;
// Use this for initialization
void Start () {
if(rigidbody)
rigidbody.freezeRotation = true;
}
void OnCollisionStay(){
Grounded = true;
}
// Update is called once per frame
void Update () {
float MoveForward = Input.GetAxisRaw("Vertical") * RunSpeed * Time.deltaTime;
float MoveRight = Input.GetAxisRaw("Horizontal") * RunSpeed * Time.deltaTime;
//Move The Player
transform.Translate(Vector3.forward * MoveForward, camera);
transform.Translate(Vector3.right * MoveRight, camera);
Answer by robertbu · Jan 12, 2013 at 06:46 AM
From this description, I'm not sure of your setup, but it sounds like you have this script either attached to the tilted camera, or to a character who is a child of the tilted camera. Try creating a parent object that has no tilt. The character and the camera can be children with whatever local rotation they need. The movement script would then be attached to the parent object which is only rotated around the Y axis. That way your forward vector will always be parallel to the ground.
i forgot to mention sorry about that but the camera can also turn to different angles all around the player so having one parallel wont have that much of an impact. it would work and i am producing a game which that answer will help tho.
Use Transform.localRotation to rotate the camera. The parent object remains level for movement, but the camera tilts/pans relative to the parent object.