Question by
a03234833 · Apr 05, 2017 at 05:16 PM ·
rotationdirectiontransform.rotationvector 3
I can't coexist by character direction of camera vector AND character rotate by Input
I have the full code of my character controller, my code can independent operation of character direction of the camera vector when the camera rotate by mouse AND Character rotate facing the input direction
but CAN'T USE THESE TWO CODE AT THE SAME COMPILED PROGRAM
the problem code is at the end.
How can I solve this problem with just modify or add some code?
Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacrterControllerTest : MonoBehaviour
{
public CharacterController MyController;
GameObject character;
// Update is called once per frame
public float Speed = 0f;
public float WalkSpeed = 3f;
public float RunSpeed = 6f;
public float GravityStrength = 5f;
public float JumpSpeed = 10f;
public Transform CameraTransform;
public Camera targetCamera;
private Camera cam;
private float currentX = 0.0f;
//private Transform m_Cam; // A reference to the main camera in the scenes transform
private Vector3 m_CamForward; // The current forward direction of the camera
float _doubleTapTime;
private void AlignToCamera()
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, targetCamera.transform.eulerAngles.y, transform.eulerAngles.z);
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//get input from the player
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
//X, Y, Z
Vector3 myVector = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 NextDir = new Vector3(moveHorizontal, 0.0f, moveVertical);
// Get the direction of the camera
Vector3 moveDirection = Camera.main.transform.forward;
// Make this vector planar (xz-plane)
moveDirection.y = 0.0f;
// Normalize to get a unit vector (optional)
Vector3.Normalize(moveDirection);
myVector = myVector * Speed * Time.deltaTime;
bool doubleTap = false;
#region doubleTap
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
{
if (Time.time < _doubleTapTime + .3f)
{
doubleTap = true;
}
_doubleTapTime = Time.time;
}
#endregion
if (Input.GetKey(KeyCode.W) == false && Input.GetKey(KeyCode.A) == false && Input.GetKey(KeyCode.S) == false && Input.GetKey(KeyCode.D) == false)
{
Speed = WalkSpeed;
}
if (doubleTap)
{
Speed = RunSpeed;
}
myVector = CameraTransform.rotation * myVector; //rotate input by direction of camera
//how do we get the jump button?
float verticalVelocity = MyController.velocity.y - GravityStrength * Time.deltaTime;
if (Input.GetButtonDown("Jump") && MyController.isGrounded)
{
//add jumpspeed to vertical velocity
verticalVelocity += JumpSpeed;
}
myVector.y = verticalVelocity * Time.deltaTime;
// use input to move the character
MyController.Move(myVector);
//transform.Translate(myVector * Speed * Time.deltaTime, Space.World);
if (myVector.x != 0 || myVector.z != 0)
{
// ↓ HERE IS THE MAIN PROBLEM!!
transform.rotation = Quaternion.LookRotation(NextDir, Vector3.up);
transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
// ↑ HERE IS THE MAIN PROBLEM!!
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612114759im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make an object rotate in the direction using horzontal and vertical input axis? 0 Answers
Object rotates normaly while moving, then does zig zags? 0 Answers
Object rotate and slowly stop to toward 1 Answer
How to get the direction based on rotation 0 Answers
Locking facing direction or changing it after following the mouse position 0 Answers