Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by jamesthornton78 · May 27, 2011 at 11:52 AM · rigidbodycharactercontrollertank

Simple Tank Controller

I'm trying to create a simple tank based game and basically I wan't to have a tank move forward with W, back with S, turn right with D and left with A in a simple world (the floor will always be the same height and the walls are quite simple) my problem is I've tried to implement it before and always had some problems with it not behaving exactly as it should. My question is what is the best practice way to do it: by using a Rigidbody and AddForce, or by using a Character Controller, or by just manually setting the positions and handling collisions myself?

Thanks, in advance :) - James

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 willgoldstone · May 27, 2011 at 12:00 PM 0
Share

Hi James, as with many issues in Unity there are many ways to approach it. Part of the solution relies on the kind of terrain / environment the tank will be covering - whats your plan? This may alter whether you should use wheel colliders, primitive colliders, raycasting.. etc?

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · May 27, 2011 at 12:59 PM

Believe me, the Character Controller is easier to use. Just add the prefab Standard Assets/Character Controllers/First Person Controller to your scene. It has built-in controls which allows left-right and forth-back movimentation, but doesn't turn. You can disable or delete the 3 built-in scripts and add this one:

 private var controller: CharacterController;
 var speed: float = 6.0;
 var turnSpeed: float = 90;

 function Start(){
 
     controller = GetComponent(CharacterController);
 }
 
 function Update(){
 
     var movDir: Vector3;
 
     transform.Rotate(0,Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime,0);
     movDir = transform.forward*Input.GetAxis("Vertical")*speed;
     // moves the character in horizontal direction
     controller.Move(movDir*Time.deltaTime-Vector3.up*0.1);    
 }

It's very simple, but does the job: it goes forth and back, turns to right and left, and DOESN'T jump, which would be very strange for a thank!

Comment
Add comment · Show 2 · 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
avatar image joshuahalls13 · Jan 12 at 04:57 PM 0
Share

Thanks for this.

For anyone who may need it, here is the script in c#:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TankMovement : MonoBehaviour
 {
     private CharacterController controller;
 
     public float speed = 6f;
     public float turnSpeed = 90f;
 
     private void Start()
     {
         controller = GetComponent<CharacterController>();
     }
 
     private void Update()
     {
         Vector3 movDir;
 
         transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
         movDir = transform.forward * Input.GetAxis("Vertical") * speed;
 
         // moves the character in horizontal direction
         controller.Move(movDir * Time.deltaTime - Vector3.up * 0.1f);
     }
 }

avatar image blokkiesotto · May 02 at 05:51 PM 0
Share

Hi Can you pls make a rigid body based code pls

avatar image
0

Answer by jgt79 · Jan 12 at 05:01 PM

I know you've already had your question answered, but thought I'd share this link. I found this very helpful.

https://www.youtube.com/watch?v=Ho6eR5-mfyA

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

6 People are following this question.

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

Related Questions

Character - Collider With Scene 0 Answers

Step Offset for Rigidbody Character 1 Answer

Character Controller Jittering 0 Answers

Should a Character Controller attach with Rigidbody? 2 Answers

How to use character controller to push down hinge joint properly 1 Answer


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