简体   繁体   中英

AI Car Unity wrong rotation

This is my first post, so if I do something wrong, let me know it. Im doing in Unity 5.5 a 2D Car Game, with a topdown view. For the game, i need that some cars have a simple AI Script, just for follow a path. So, I searched in the web something that fits my needs, and found(in my opinion) a really good implementation. Here i let u the project in GitHub, uploaded by the owner of the implementation I suppose: https://github.com/mindcandy/lu-racer

So in a few words, we create a Path with empty GameObjects and the respective colliders, we have to have a car(obviously) and set up with a collider and a rigidbody2d, and we have three scripts that "do the magic". For my project, I remove the part of counting the laps because my game is not a race. Well, i set up all and everything works fine! Except for one thing: the car´s rotation.

I think this is happens because the tutorial game is a topdown view but cars go from left to right, and in my game is topdown also but cars go from down to up. So the cars's sprites in my project are in topdown view and looking to up, and the car's sprite of the github project are topdown view looking to right(i wanted upload the images but appears me that i cant do it due my reputation, sorry).

Definitely, the AI works really good and the car follow without problems the path, but with a wrong rotation, like this: aicar_rotation

I tried changing things in the AICarMovement scripts related to the vectors, and angles, but without luck, so if anyone can look and give me a hand, I'll be grateful. If anyone wants more details for understand the problem, let me know. I try upload more things like pictures or gifs to show the problem, but i cant do it due the reputation.

This is the part of code in AICarMovement that i think i have to change:

public class AICarMovement : MonoBehaviour {
public float acceleration = 0.5f;
public float braking = 0.3f;
public float steering = 4.0f;
private Rigidbody2D rigidb;

Vector3 target;

void Start() {
    rigidb = GetComponent<Rigidbody2D>();
}

public void OnNextTrigger(TrackLapTrigger next){
    target = Vector3.Lerp(next.transform.position - next.transform.right, next.transform.position + next.transform.right, Random.value);
}

void steerTowardsTarget() {
    Vector2 towarNextTrigger = target - transform.position;
    float targetRot = Vector2.Angle(Vector2.right, towarNextTrigger);
    if(towarNextTrigger.y < 0.0f) {
        targetRot = -targetRot;
    }
    float rot = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, targetRot, steering);
    transform.eulerAngles = new Vector3(0.0f, 0.0f, rot);
}

void FixedUpdate(){
    steerTowardsTarget();
    float velocity = rigidb.velocity.magnitude;
    velocity += acceleration;
    rigidb.velocity = transform.right * velocity;
    rigidb.angularVelocity = 0.0f;
}

}

Sorry about my english, is not my native language.

在steerTowardsTarget()的第二行:尝试将Angle()的第一个参数从Vector2.right更改为Vector2.up。

Turn your sprite and also the spawn game object. This way you have no fancy behaviour and all looks correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM