x


Problem with moving gameObject, especially diagonally!

I am trying to simply character controller without using a rigidbody (since I don't need any physics.) The results are ok, but for some reason my gameObject moves faster when moving diagonally than when just heading in a single direction. Here is my code:

void FixedUpdate() {    
        float xPos = Input.GetAxis("Horizontal") * _player.speed * Time.deltaTime;
        float yPos = Input.GetAxis("Vertical") * _player.speed * Time.deltaTime;

        Vector3 newPos = new Vector3(xPos, 0, yPos);
        newPos = Camera.main.transform.TransformDirection(newPos);
        _position = _position + newPos;
        _position.y = _player.transform.position.y;

        _direction = new Vector3(xPos, 0, yPos);
        _direction = Camera.main.transform.TransformDirection(_direction);
        _direction.y = 0;
    }

    void Update() { 
        if(_direction.sqrMagnitude > 0.01)
            _player.transform.rotation = Quaternion.Slerp (_player.transform.rotation, Quaternion.LookRotation (_direction), Time.deltaTime * 10);

        _player.transform.position = Vector3.Slerp(_player.transform.position, _position,Time.deltaTime*10);
    }

If anyone has any suggestions as to how I can get around this problem or perhaps even improve the above code, I'd really appreciate it!

more ▼

asked Jun 20 '10 at 05:33 PM

Disaster gravatar image

Disaster
482 48 57 67

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Change the first three lines to this:

    float xPos = Input.GetAxis("Horizontal");
    float yPos = Input.GetAxis("Vertical");

    Vector3 newPos = new Vector3(xPos, 0, yPos);
    if( newPos.magnitude > 1.0f ) 
    {
         newPos = newPos.normalized;
    }
    newPos *= _player.speed;
    newPos *= Time.deltaTime;

Since Input goes from -1 -> 1, you'll want to handle the case where x and y are both 1. The length of that is sqrt(2) if you do the trig. You want to just clamp it to 1.

more ▼

answered Jun 20 '10 at 05:51 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

Thanks a lot, this works great. I also found this code on the forums which has much the same effect... I don't think one is any faster than the other but others may find this useful:

float xPos = Input.GetAxis("Horizontal") * _player.speed * Time.deltaTime;

float yPos = Input.GetAxis("Vertical") * _player.speed * Time.deltaTime;

float inputModifyFactor = (xPos != 0.0f && yPos != 0.0f) ? .7071f : 1.0f;

Vector3 newPos = new Vector3(xPos*inputModifyFactor, 0, yPos*inputModifyFactor);

Jun 20 '10 at 05:59 PM Disaster

That will still have the same problem, except instead you'll max out at speed instead of sqrt(2) * speed.

Jun 20 '10 at 06:06 PM Tetrad
(comments are locked)
10|3000 characters needed characters left

Diagonally u mean when u press 2 keys like w + d in the same time? if yes, u can make a check and if it matches then to decrease the speed by 2.

more ▼

answered Jun 20 '10 at 05:38 PM

AnaRhisT gravatar image

AnaRhisT
865 30 33 43

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3740
x2091

asked: Jun 20 '10 at 05:33 PM

Seen: 1440 times

Last Updated: Jun 20 '10 at 05:33 PM