Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TiodoIsChinese · Dec 04, 2021 at 05:44 PM · movementcolliderstransform.position

Preventing glitching through colliders

I'm making an open world city game, and have began implementing planes to fly around. However the plane controller script makes the planes colliders be able to just glitch through other colliders. I'm pretty sure this is because I'm using this to move the plane in the direction its facing:

 transform.position = transform.position + (transform.forward * currentForwardSpeed * Time.deltaTime * -1);

This line of code just makes the plane move forward, without accounting for any object in front, making it glitch through colliders. Is there different way of moving the plane so that it interacts with other colliders, while still being able to fly like it would in this script (I assume it would be physics based, and not simply changing the objects position, but I don't know how to do that)?

The solution may be simple, but I'm fairly new, and have only been working in 3d for a few weeks, so I don't have a clue what I'm doing.

Here is my full script just in case:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShipController : MonoBehaviour
 {
     public float forwardSpeed = 25f, turnSpeed = 7.5f;
     public float currentForwardSpeed;
     public float forwardAccel = 2.5f, turnAccel = 2f;
 
     collisionCheck check; //has bool isTouching to check if ship is touching any other colliders
 
     public float lookRotateSpeed = 90f;
     private Vector2 lookInput, screenCentre, mouseDistance;
     
     void Start()![alt text][1]
     {
         check = this.gameObject.GetComponent<collisionCheck>();
         screenCentre.x = Screen.width * 0.5f;
         screenCentre.y = Screen.height * 0.5f;
     }
 
     // Update is called once per frame
     void Update()
     {
         lookInput.x = Input.mousePosition.x;
         lookInput.y = Input.mousePosition.y;
 
         mouseDistance.x = (lookInput.x - screenCentre.x) / screenCentre.y;
         mouseDistance.y = (lookInput.y - screenCentre.y) / screenCentre.y;
 
         mouseDistance = Vector2.ClampMagnitude(mouseDistance, 1f);
 
         transform.Rotate(mouseDistance.y * lookRotateSpeed * Time.deltaTime, mouseDistance.x * lookRotateSpeed * Time.deltaTime, 0f, Space.Self);
 
         currentForwardSpeed = Mathf.Lerp(currentForwardSpeed, Input.GetAxisRaw("Vertical") * forwardSpeed, forwardAccel * Time.deltaTime);
         
         this.GetComponent<Rigidbody>().useGravity = false;
         transform.position = transform.position + (transform.forward * currentForwardSpeed * Time.deltaTime * -1);
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(0, 0, turnSpeed * Time.deltaTime * -1);
         }
         if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(0, 0, turnSpeed * Time.deltaTime);
         }
     }
 }
 


[1]: /storage/temp/189548-shipglitch.png

shipglitch.png (36.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FuryFight3r · Dec 04, 2021 at 11:05 PM 0
Share

You will need to add a Rigidbody and Collider to the plane, though by wanting the plane to interact with world objects it means you will need to restructure the entire method for the planes flight, rather than simply moving forward (I personally have never worked with a flying object) though I can only assume it is similar to ground vehicles in the same sense that if you force the translation of the vehicle to move forward according to a button/mouse press it will always phase through colliders, however if you use propulsion/velocity on the wheels the grip of the wheels wont be 100% and crashing into a wall/building will simply make the wheels slip rather than forcing the vehicle through the wall/building, all I can suggest is make the plane have some sort of Rigidbody velocity, which again I have never worked with flying Rigidbodies so I am unable to provide an example, though I have worked with Physical Bullets that have an initial velocity upon release and slowly decrease in velocity over time, in the case of a Plane it may be similar however you will need to be constantly adding velocity to keep the plane flying and not slowly drop out of the sky

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pathojen · Dec 06, 2021 at 03:21 PM

I'm assuming what you want is to move forward automatically, and left and right from keyboard keys "a" and "d". would try using transform.Translate (whatever direction) instead of transform.position. Translate will react and collide with other colliders, so long as it is not kinematic. Something similar to this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Flight : MonoBehaviour
 {
 
     public float speed;
     public float rotSpeed;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
         transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
     }
 }

Input.GetAxis("Horizontal)" and Input.GetAxis("Vertical") is automatically set to control things based on the a and d, and w and s keys respectively, as well as left and right, and up and down.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

192 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move an object smoothly while in a Coroutine / Ienumerator? 3 Answers

Vector3 of moving object always returns (0,0,0) 1 Answer

Player Sphere Collider randomly bounces up 1 Answer

Bullet fired at enemy only applying damage when the enemy moves 1 Answer

Setting transform.position makes object appear in wrong place sometimes. Possible conflict with colliders/rigidbody? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges