Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character controller movement while looking

Discussion in 'Scripting' started by Lando9000, Dec 8, 2014.

  1. Lando9000

    Lando9000

    Joined:
    Apr 14, 2013
    Posts:
    36
    Hey peeps,

    I was wondering if someone could tell me what i am doing wrong I'm assuming its my lack of knowledge with the character controller, I'm making a game that is top down, looking down the Y axis so up is Z and right is X. I want to look at an enemy while I'm moving and i have it working but theres some really strange resistance and movement as I circle the player. My code is below, if someone could point out where I'm going wrong that would be great!

    Vector3input = newVector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

    targetRotation = Quaternion.LookRotation(enemyTrans.position - transform.position);


    Vector3 movement = input;
    movement *= (Mathf.Abs(input.x) == 1f && Mathf.Abs(input.z) == 1f) ? 0.7f : 1f;
    movement += Vector3.up * -8f;
    movement *= MoveSpeed;
    controller.Move(movement * Time.deltaTime);

    transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, RotationSpeed * Time.deltaTime);
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Can't tell without seeing it (post web player demo) but my random guess would be that Quaternion.LookRotation is assuming a up direction of Vector3(0,1,0) when you possibly want up to be (1,0,0) or Vector3.forward. You can pass the up direct vector as a second argument to Quaternion.LookRotation.

    Logically, you can't really set a rotation with only 1 direction in space and no other reference points, thus Unity will assume you want the camera upright and looking towards that direction. You need at least two directions form which you can then infer the third (forward x up = right) and construct a rotation from it.
     
  3. Lando9000

    Lando9000

    Joined:
    Apr 14, 2013
    Posts:
    36
    okay thanks i will have have a gander, just to clarify my character always looks at the enemy perfectly its just the movement so i assumed it was my Vector3 movement value moving the character controller? controller.Move(movement * Time.deltaTime);
     
  4. Lando9000

    Lando9000

    Joined:
    Apr 14, 2013
    Posts:
    36
    Ive been silly, realised i was doing it inside FixedUpdate instead of Update. Doh!!!