Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

A Little Project

on Thu Sep 08, 2016 10:17 pm
Hello,

I am still very new to making games; not too long ago I completed a beginner's tutorial for a simple game called 'roll a ball' on Unity 5. I've taken some of the concepts from that game and applied them to my current project that will be the second little game I've made, but in this game I want to make the player jump.
I'm scripting in CSharp. I've already added a rigidbody to my player and attached a script; right now the script is just the sample starter script and I haven't done anything to it yet:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}


And my player object is a cylinder (instead of a sphere like in roll a ball) and my game is 3D, and I would like to use the spacebar button for the jump and the arrow keys for movement, and I want the jump to just be a single jump and not a double jump.
I'm also still a complete novice in scripting.
If you could tell me the code for this, I would appreciate your help very much!  Very Happy
I think I'll need help for making the camera follow the player like it would in a standalone platformer adventure game, but one thing at a time. [-=
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Try this

on Sat Sep 10, 2016 12:30 am
Hello
See if you like this little code I wrote for you.
You can edit the movement keys adding arrows moves.
I hope you greetings aid Very Happy


Code:
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
 
 public Rigidbody rb;
 public float speed = 2f;
 public float jump = 5f;
 bool isFalling =  true;

 void Start () {
 rb = GetComponent<Rigidbody> ();
 }
 
 
 void Update () {
 //right
 if(Input.GetKey(KeyCode.D)){
 rb.MovePosition(transform.position + Vector3.right * Time.deltaTime * speed);
 }
 //left
 if(Input.GetKey(KeyCode.A)){
 rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
 }
 //forward
 if(Input.GetKey(KeyCode.W)){
 rb.MovePosition(transform.position + Vector3.forward * Time.deltaTime * speed);
 }
 //back
 if(Input.GetKey(KeyCode.S)){
 rb.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
 }
 //jump---------------
 if(Input.GetKey(KeyCode.Space) && !isFalling){
 isFalling = true;
 rb.velocity += transform.up  * jump;

 }

 }

 void OnCollisionStay(Collision collinfo){
 isFalling = false;
 }



}
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Thanks!

on Sat Sep 10, 2016 3:53 pm
This code worked good, thanks for the help! [-= PS I'm gonna stick with the W,A,S,D key setup, I like it better. [-=
Now I'm trying to get my player to pick up my collectibles. And since I'm trying to execute certain concepts from the 'roll a ball' tutorial into my game, I've been using some of the code but adapting it slightly (e.g. calling the collectible item "Gem" instead of "Pick Up"). Here's the code that makes the collectibles rotate, just in case you need it:

using UnityEngine;
using System.Collections;

public class Gems : MonoBehaviour {

// Update is called once per frame
void Update () {
{
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
}
}
}


And here's the code that I've added to my Playercontroller script:

{    
   void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Gem"))
{
other.gameObject.SetActive (false);
}
}
}


And here's my PlayerController script as a whole:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public Rigidbody rb;
public float speed = 2f;
public float jump = 5f;
bool isFalling =  true;

void Start () {
rb = GetComponent<Rigidbody> ();
}


void Update () {
//right
if(Input.GetKey(KeyCode.D)){
rb.MovePosition(transform.position + Vector3.right * Time.deltaTime * speed);
}
//left
if(Input.GetKey(KeyCode.A)){
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
}
//forward
if(Input.GetKey(KeyCode.W)){
rb.MovePosition(transform.position + Vector3.forward * Time.deltaTime * speed);
}
//back
if(Input.GetKey(KeyCode.S)){
rb.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
}
//jump---------------
if(Input.GetKey(KeyCode.Space) && !isFalling){
isFalling = true;
rb.velocity += transform.up  * jump;

}

}

void OnCollisionStay(Collision collinfo){
isFalling = false;
}
}


{    
   void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Gem"))
{
other.gameObject.SetActive (false);
}
}
}


So next to the '{' that appears above 'void OnTriggerEnter (Collider other)' there's sometimes a little red line indicating an error, and in Unity in the console it says,
'Assets/Materials/Scripts/PlayerController.cs(48,1): error CS8025: Parsing error' and when I double click it it takes me to that '{' that's above 'void OnTriggerEnter (Collider other)' and the '{' has a little red line next to it and the typing cursor is right behind the '{'.

I tried a few different things, but it either didn't solve the problem or caused another problem (or another way to put it, I managed to fix one problem but then got another).

Once again thanks so much for your help, and I greatly appreciate your further assistance! (=
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sun Sep 11, 2016 2:44 am
Dear
You messed with the braces "{}"
Always remember

"void pippo () {
} "

1) opens a "{"
1) closing "}"

I've corrected the script watch it and try
 Wink


Code:
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public Rigidbody rb;
public float speed = 2f;
public float jump = 5f;
bool isFalling =  true;

void Start () {
rb = GetComponent<Rigidbody> ();
}


void Update () {
//right
if(Input.GetKey(KeyCode.D)){
rb.MovePosition(transform.position + Vector3.right * Time.deltaTime * speed);
}
//left
if(Input.GetKey(KeyCode.A)){
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
}
//forward
if(Input.GetKey(KeyCode.W)){
rb.MovePosition(transform.position + Vector3.forward * Time.deltaTime * speed);
}
//back
if(Input.GetKey(KeyCode.S)){
rb.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
}
//jump---------------
if(Input.GetKey(KeyCode.Space) && !isFalling){
isFalling = true;
rb.velocity += transform.up  * jump;

}

}

void OnCollisionStay(Collision collinfo){
isFalling = false;
}


   void OnTriggerEnter (Collider other){

if (other.gameObject.CompareTag("Gem")){
other.gameObject.SetActive (false);
}
}
}
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sun Sep 11, 2016 3:00 am
if you look on the forums you will find much material that you could serve.

"Books"
http://www.unityninja.net/t881-free-unity-ebooks-download-for-free-great-unity-books-free

"Tutorials"
http://www.unityninja.net/f5-tutorials

I forgot not to defeat the other.gameObject.SetActive gems (false);
Destroy Game Object
Otherwise you away memory and resources because the object and just not visible.

Hello good job
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Odd Physics

on Wed Sep 14, 2016 7:46 pm
I got another part of the project squared away; I can now pick up my collectibles! Very Happy But now I'll need help to address a physics issue.

The issue is this: whenever my player (a cylinder) runs into an object, it doesn't just run into it (as it should) but it also bounces off and knocks the player down and off tilt; my player will spin about and sometimes land in the sideways position, sometimes teeter in the upright position, sometimes be on its side and facing diagonally, and a jump will send it ricocheting across the playing area. The player also has this response when coming in contact with the top of a platform (in this case the platforms are cubes and cylinders) and other times still the player will come to fall over when it hasn't run into anything and has simply been scaling the ground (the ground being a plane).

I tried turning on 'Is Kinematic' in the player's Rigidbody (where I also have 'Use Gravity' turned on) and it did stop the falling and out of control collisions, but it also caused me to go right through the platforming pillars and took away my ability to jump, so I unchecked it.

I'm guessing my issues have to do with the current settings on my game's objects, but I'm not sure what my settings need to be.

Once again I really appreciate the help, and if you need more info let me know! Very Happy

PS Even with 'Is Kinematic' unchecked, my player still goes right through the trees. Do you think this could become an issue?
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Thu Sep 15, 2016 1:39 pm
Hello
Try to put Start this.

Code:

void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Same Code Issue

on Thu Sep 15, 2016 7:25 pm
I took your bit of code and put it in my PlayerController script, but once again I ran into the same coding problem as before; when the snippet looks like this:

void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}


and there's no beginning brace in front of 'void' it puts a red line under void and says 'error CS0116: A namespace can only contain types and namespace declarations'.
But when I try to create the corresponding brace pairs (having each beginning brace have an end one and vice versa) and to put a beginning brace next to 'void' like this:

{ void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}


Or above 'void' like this:

{
void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}


It says, 'error CS8025: Parsing error'. So I then I try giving the beginning brace above 'void' an ending brace below:

{
void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}
}


And it still has the same error on the console. So basically I keep trying to add on brace pairs and place them in different places and trying to get it to look correct but nothing's working; it just keeps having that parsing error on the top line with the beginning brace.
Here's the PlayerController code to this point:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public Rigidbody rb;
public float speed = 2f;
public float jump = 5f;
bool isFalling = true;

void Start () {
rb = GetComponent<Rigidbody> ();
}


void Update () {
//right
if(Input.GetKey(KeyCode.D)){
rb.MovePosition(transform.position + Vector3.right * Time.deltaTime * speed);
}
//left
if(Input.GetKey(KeyCode.A)){
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
}
//forward
if(Input.GetKey(KeyCode.W)){
rb.MovePosition(transform.position + Vector3.forward * Time.deltaTime * speed);
}
//back
if(Input.GetKey(KeyCode.S)){
rb.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
}
//jump---------------
if(Input.GetKey(KeyCode.Space) && !isFalling){
isFalling = true;
rb.velocity += transform.up * jump;

}

}

void OnCollisionStay(Collision collinfo){
isFalling = false;
}


void OnTriggerEnter (Collider other){

if (other.gameObject.CompareTag("Gem")){
other.gameObject.SetActive (false);
}
}
}


{
void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}
}


What am I doing wrong? I'm trying to line everything up right and make sure each brace has a corresponding one, and I'm also trying to copy the code you give exactly, so why do I keep having the same issue?
Thanks a lot again for all your help and patience (= I hope I don't have to bother you for help on this issue again after this =-(
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sat Sep 17, 2016 1:24 pm
But you have the Start() affraid
find him before Update ()
you just had to add rb.freezeRotation = true; them inside
Greetings
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Sorry..

on Sat Sep 17, 2016 5:30 pm
Sorry, I don't completely understand what you mean by,

'But you have the Start()
find him before Update ()
you just had to add rb.freezeRotation = true; them inside'

I tried to figure it out and tried a few different things in the code but I couldn't get it. Are you saying I need a value for 'rb.freezeRotation = true;'?
Right now I've got the code snippet set back to the way it was when you gave it to me:

void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}


So now I'm back to this error again:
'error CS0116: A namespace can only contain types and namespace declarations'
and then when I hover over the red line below the 'void' it says "Parser error: unexpected symbol 'void'".

Here's the code as a whole again just in case it helps:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public Rigidbody rb;
public float speed = 2f;
public float jump = 5f;
bool isFalling = true;

void Start () {
rb = GetComponent<Rigidbody> ();
}


void Update () {
//right
if(Input.GetKey(KeyCode.D)){
rb.MovePosition(transform.position + Vector3.right * Time.deltaTime * speed);
}
//left
if(Input.GetKey(KeyCode.A)){
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
}
//forward
if(Input.GetKey(KeyCode.W)){
rb.MovePosition(transform.position + Vector3.forward * Time.deltaTime * speed);
}
//back
if(Input.GetKey(KeyCode.S)){
rb.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
}
//jump---------------
if(Input.GetKey(KeyCode.Space) && !isFalling){
isFalling = true;
rb.velocity += transform.up * jump;

}

}

void OnCollisionStay(Collision collinfo){
isFalling = false;
}


void OnTriggerEnter (Collider other){

if (other.gameObject.CompareTag("Gem")){
other.gameObject.SetActive (false);
}
}
}


void Start () {
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
}


Sorry I couldn't understand what you meant the first time! =-/

avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sun Sep 18, 2016 1:45 am
Double check the listing and watch the video that you link.
If you want to use unity least you have to learn the syntax for the scripts. Very Happy

https://www.youtube.com/watch?v=DaE6L2OMi60
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Thanks!

on Sun Sep 18, 2016 2:27 pm
Will do, thanks!
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Next Phase

on Sun Sep 18, 2016 6:32 pm
Now I just need a script for the camera. I would like it to follow the player around the way it would in a third person RPG.
Thanks, and sorry if I'm mooching off of your knowledge, I'm not trying to. I plan on learning CSharp from scratch after I finish this project and before I try to make any more games; I think I'm getting closer to done with this project. Again I hope I'm not bothering you and I'm not trying to use you or anything like that; if i knew how to make credits for the game I'd put your screen name in them! [-=
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sat Sep 24, 2016 2:07 pm
Hello
You must not in any way enter any credit.
try if you like this script for Maincamera.
Wink



Code:

/*
**************************
      FollowCamera
**************************
*/
public var distance = 10;
public var targetHeight = 2.0;
private var target : Transform;
private var x = 0.0;
private var y = 0.0;
 
function Start () {
  
    target = GameObject.FindGameObjectWithTag("Player").transform;
    var angles = transform.eulerAngles;
    x = angles.x;
    x = 25;
    y = angles.y;
    
}
 
function LateUpdate () {
   if(!target)
      return;
      y = target.eulerAngles.y;
 
   var rotation:Quaternion = Quaternion.Euler(x, y, 0);
   transform.rotation = rotation;

   var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
   transform.position = position;
 
}
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Awesome! Just Trying to fix Errors

on Sun Sep 25, 2016 2:15 am
Thanks a ton for the code, and don't worry I won't do any credits now that you told me not to.

So I pasted the script into my PlayerController script, and the thing I'm using for the script, the Unity MonoDevelop, seems to have a problem with the 'var' in 'public var distance = 10;' and this seems to produce 3 different errors. When I hover my cursor over the red line underneath the 'var' it says "unexpected symbol 'var' expecting 'class', 'delegate', 'enum', 'interface', 'partial', or 'struct'" so my question is, which one should I pick and try out for this instance and this script: class, delegate, enum, interface, partial, or struct?
Also, am I supposed to attach this script to the PlayerController script? Did I get that right?

Thanks again help forum buddy! [-=

PS can I give you credit for all the code and help you gave me as far as just telling people, like when I say 'hey I made this little game' can I add something like 'but I couldn't have done it without so-and-so's help from the help forum'?
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 53
Points : 1530
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

Re: A Little Project

on Sun Sep 25, 2016 11:38 am
No this is one java script is pasted into a new Java script name you want.
and then drag in MainCamera.
and it's done.
Regards
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Awesome

on Sun Sep 25, 2016 2:13 pm
It worked, thanks! [-= I didn't know you could have different scripting languages work together like that-neat! [-=
Member [Lv. 2]
Member [Lv. 2]
Posts : 10
Points : 773
Rep : 0
Join date : 2016-09-08
View user profile

Finished!

on Sun Sep 25, 2016 7:37 pm
The game is done! Thanks so much again for all your help, and unless you don't want me to I'll tell everyone that I show/mention the game to that I couldn't have done it without all your help! Very Happy
Back to top
Permissions in this forum:
You cannot reply to topics in this forum