UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


A Little Project

power_settings_newLogin to reply
2 posters

descriptionA Little Project EmptyA Little Project

more_horiz
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. [-=

descriptionA Little Project Empty Try this

more_horiz
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;
 }



}

descriptionA Little Project EmptyThanks!

more_horiz
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! (=

descriptionA Little Project EmptyRe: A Little Project

more_horiz
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);
}
}
}

descriptionA Little Project EmptyRe: A Little Project

more_horiz
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

descriptionA Little Project EmptyOdd Physics

more_horiz
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?

descriptionA Little Project EmptyRe: A Little Project

more_horiz
Hello
Try to put Start this.

Code:


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

descriptionA Little Project EmptySame Code Issue

more_horiz
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 =-(

descriptionA Little Project EmptyRe: A Little Project

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

descriptionA Little Project EmptySorry..

more_horiz
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! =-/

descriptionA Little Project EmptyRe: A Little Project

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply