Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mcabral89 · Oct 28, 2015 at 01:21 AM · c#movementclassesinputmanagerinput.getkey

Using a class to control two "player" objects

Hi all! I'm working on some homework and i'm looking for a little guidance. Anyone that's been through the forums and/or YouTube has seen the "roll-a-ball" tutorial. For my intro to c# class, i'm having to create my own modified version of this game. I already have one version with a single player, 3 enemies, and a win condition. That version works. My task in this version is to add a second player that moves using separate inputs, and i have to create classes for my two players and enemies. What is the best way to approach this? I want to only focus on the player at the moment. Once i figure out what i need to do i can apply it to the enemies later.

  1. I have several triggers within my current playerController script that i need to function. Can that be part of the class? It will need to be part of both players interaction with the environment.

  2. Since both of my players will be receiving different inputs to govern their movements, should i create a simple script for both that only handles the movement of the players and let the playerController/class script handle everything else? My assignment does specifically say "these classes should have the ability to move in the world". How do i designate different inputs for two objects of the same class?

Also, can input manager be used to solve some of my questions? I've been looking through it but all the settings seem to be general settings that apply to all objects, not just a single object.

I hope you guys can point me in the right direction.

Comment
Add comment · Show 2
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 mcabral89 · Oct 28, 2015 at 07:16 PM 0
Share

This is my movement script for my two "players". using UnityEngine; using System.Collections;

public class player$$anonymous$$ovement : $$anonymous$$onoBehaviour {

 Rigidbody rb;
 float moveHorizontal;
 float moveVertical;
 public float speed;



 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     if (this.gameObject.tag == "Player1") 
     {
         moveHorizontal = Input.GetAxis ("Horizontal");
         moveVertical = Input.GetAxis ("Vertical");

         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce (movement * speed);
     }

     if (this.gameObject.tag == "Player2")
     {
         moveHorizontal = Input.GetAxis ("P2Horizontal");
         moveVertical = Input.GetAxis ("P2Vertical");

         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce (movement * speed);
     }






 }

} What i need to know is how can I create a class that governs my two objects using this code? When I get rid of the the "mono behavior" inheritance to create the class the script stops working. Also, should i move the "moveHorizontal = Input.GetAxis ("Horizontal");" variable assignment to the object itself and pass the "moveHorizontal" variable into the class? I've tried it previously without success.

avatar image mcabral89 · Oct 28, 2015 at 07:27 PM 0
Share

okay.... so i can't add anything to an object in unity that isn't derived from $$anonymous$$ono Behavior. Does that mean that i will not be able to have my movement script as part of the class?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali-hatem · Oct 28, 2015 at 11:13 AM

 //tag each gameobject in hierarchy & test its tag in the same script:
 if (this.gameobject.tag == "player")
 {
       //do something
 }
 
 if (this.gameobject.tag == "enemy")
 {
       //do something
 }
 
 

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 mcabral89 · Oct 28, 2015 at 07:09 PM 0
Share

Thank You Ali! That helped with one of my problems. I can now move the players independently with different key bindings.

avatar image Ali-hatem mcabral89 · Oct 29, 2015 at 03:50 PM 0
Share

@mcabral89 : i am sorry i didn't read the class example its like my answer already in your script . but i still not understand what is the problem if you just attach this script to all the game objects it will work fine and you don't need a reference to the game object just use rigidbody.AddForce and delete rb.

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

32 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

Related Questions

Input.GetAxisRaw always returns -0.6 1 Answer

c# - Make something happen if I press a sequence of keys 4 Answers

Move only if mouse click the Terrain 0 Answers

Movement input problem? 1 Answer

2D Object following the cursor without glitchyness at the center 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