- Home /
Other
Ship and terrain collision.Need help.
Hello
I am creating a smalll game where you can control ship and a chracter.
My problem is i have created islands using Unity terrain and I want the ship to collide with the terrain (Stop moving when it hits terrain) instead the ship passes right through it.
In my script i move the ship with transform.Translate and I move the player with Character controller.
I have tried to move the ship with rigidbody but then the collisions don't work with the player. I have also tried to move the ship with character controller but than collisions only work on the part of the ship where the character controller reaches with its radius.
Anybody knows how would i create these collisions ?
Answer by the_genius · Apr 30, 2018 at 12:12 PM
Transform.Translate just changes the transform.position to move an object. This is like 'teleporting' the object every frame so it will 'teleport' through the terrain without the Physics system interacting at all.
I suggest you use a rigidbody and mesh collider. In your scripts use Rigidbody.AddForce rather than Transform.Translate.
Thank you for your help.I have managed to fix this.
Here is the whole ship script feel free to use it
public float turningSpeed;
public float maxSpeed;
public float acceleration;
float $$anonymous$$Speed = 0f;
public float speed;
Vector3 movement;
Rigidbody rgBody;
// Use this for initialization
void Start ()
{
rgBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
float leftAndRight = Input.GetAxis("Horizontal");
float forwards = Input.GetAxis("Vertical");
speed += forwards * acceleration * Time.fixedDeltaTime;
if(speed > maxSpeed)
{
speed = maxSpeed;
}else if(speed < $$anonymous$$Speed)
{
speed = $$anonymous$$Speed;
}
$$anonymous$$ove(forwards);
Turning(leftAndRight);
}
void Turning(float leftAndRight)
{
float turn = leftAndRight * turningSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rgBody.$$anonymous$$oveRotation(rgBody.rotation * turnRotation);
}
void $$anonymous$$ove(float forwards)
{
movement.Set(0f, 0f,forwards);
Vector3 move = transform.forward * forwards * speed * Time.fixedDeltaTime;
rgBody.$$anonymous$$ovePosition(rgBody.position + move);
}
}
Follow this Question
Related Questions
Terrain Detail Mesh Collisions 1 Answer
bullet collision with Terrain and Character Collider doesn't work in very fast speed 2 Answers
Collision Problem - Terrain collider remains flat after raising or lowering it 0 Answers
Crawling around the terrain 3 Answers
Character controller falling down 1 Answer