Unity3D: Player accelerates indefinitely when jumping - c#

Hello people,
I have a problem with my PlayerController in Unity3D. As stated in the title my player accelerates indefinitely when pressing the "jump"-key. My real problem here is, that this is only the case if I export the scenes and then start the game. If I just start the scene in the Unity Editor everything just works fine. So its probably not the code?
I do recall, that a friend of mine experienced the same problem a while ago while testing one of the earlier versions of the game while I couldn't reproduce it on my machine. Unfortunatly he didn't have time to review this version until now.
Just since I switched to a new developing station I experience that problem.
If I run the game on my laptop it runs just fine (not mentioning the 3 FPS I have, but it does work ^^).
All 4 PCs mentioned above run Windows 10, if that helps.
This is the code I use for my jump method:
private void Jump()
{
if (groundDetector.currentCollisions.Count != 0)
{
Vector3 vel = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
rigidbody.velocity = vel;
Vector3 jumpForce = new Vector3(0, jumpPower, 0);
rigidbody.AddForce(jumpForce, ForceMode.Impulse);
}
}
I would be really thankfull if someone could help my with this problem, since I already spend some hours of googling time and didn't find a clue at all, noone seems to have the same problem...
Thanks in advance!
Lukas

Related

Unity3d crashes every time I run my game because of the spawner Game object spawning Enemy prefabs

I am about half way done with the firs prototype of my new game-idea, and I am tring to implement a spawner that randomly spawns one of 3 enemy prefabs. This enemy spawning hasn't been working very well for me, as I have always found little problems here and there. One of these problems was major and caused Unity to crash every time I pressed Play ind the editor. I wasn't sure what the problem was, and so I Build the game I tried running the build, and it crashed as well. Then eventually I scraped my spawner and tried a new one. This one also crashed Unity every time, so I tried to spawn a random sphere with no components to it, and this seems to be working perfectly fine.
While running the game In the Unity Editor, I simultaneously open the task manager to see what was going on. It told me Unity was running like a charm, although it used about 1.5 gigs of Ram. Is it a lot? TBH, I don't know if that's the reason it keeps crashing, but I wouldn't know why it takes these amounts of ram. As soon as I deactivated the spawner it stopped crashing, so is it the spawner? I personally don't think so because I was able to spawn the sphere without the game crashing, but as soon as I try to spawn the enemies breaks down.
-> edit: The game doesn't really crash, it just starts loading into infinity, forcing me to end the application and reopen it hands why I am not able to provide any error codes.
Here is the spawner script I used:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] enemyPrefabs;
public float spawnRate = 1.0f; //The frequency at which enemies spawn.
private float nextSpawn = 0.0f; //used to trigger the next spawn
void Update()
{
//Make enemy spawn on a countdown timer
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate; // Trigger next spawn
Debug.Log("Spawn");
//Spawn the enemy in the same place as the spawner
GameObject piClone = Instantiate(enemyPrefabs[Random.Range(0, enemyPrefabs.Length)], transform.position, transform.rotation) as GameObject;
}
}
}

Controlling navmesh agent rotation resulting in odd behavior

I have a navmesh agent, I was not happy with how it updated its rotation (was always slightly off), so I turned it off and did it my self.
It works really nicely, however when I run the game at 3-4 times the speed (management game), they will sometimes lay down flat for 0,5-1 seconds then keep going, I'm assuming it's caused by this script.
Im pretty sure its when they are standing still or just about to turn.
void LateUpdate()
{
if (agent.velocity.sqrMagnitude > Mathf.Epsilon)
{
transform.rotation = Quaternion.LookRotation(agent.velocity.normalized);
}
}
Apparently it was happening while calculating a path, so adding this solved it:
void LateUpdate()
{
if (agent.hasPath)
{
if (agent.velocity.sqrMagnitude > Mathf.Epsilon)
{
transform.rotation = Quaternion.LookRotation(agent.velocity.normalized);
}
}
}

Issue with moving clients/players with Photon Networking in Unity

I'm making a game where players are put onto plates and random events happen to the plates/players. I've gone through a multitude of different networking solutions like UNET, Mirror, Mirror+Steamworks P2P but none of them really worked well (main issue being not being able to join via IP) so I settled on Photon. After Punv2 just wouldn't work on my project (an error in PhotonEditor.CS) I just used Punv1 which is working perfectly for the most part.
The issue right now is that I'm trying to spawn players on their owned plates but they only spawn on the first plate despite each plate being owned by a player (each plate has a script that specifies which player 'owns' it. This is being set correctly). This only seems to happen when I try to test with a real player/client. If I create a fake player by just spawning in the prefab and then running it, both players will be moved to their correct plate so it seems to be a networking issue.
Here's the code responsible for moving the players to their plates.
foreach (GameObject plate in spawnedPlates)
{
//Here we loop through each plate, get the player assigned to it and move the player to that plate.
GameObject player = plate.GetComponent<Plate>().assignedTo;
PlayerClientControl playerController = player.GetComponent<PlayerClientControl>(); // originally for UNET/Mirror. Left in incase I need it(had an RPC function that moved the player).
player.transform.position = plate.transform.position + new Vector3(0, 4, 0);
}
What am I doing wrong?
EDIT:
It seems they ARE being moved to the correct positions (or atleast, it's trying to move them to the correct position) via a debug print statement that prints where they are trying to be teleported to further cementing that this is a networking issue but I have no idea how to fix it.
I assume it's something to with host/client synchronization? If anyone could shed some light on this, that'd be great because I'm pulling my hair out over this.
If you inspect the player objects at runtime, it will probably give you a clue. I had the same and in my case I had InputAction attached to my players (taken from unity StarterAssets). The first player in the game was assigned keyboard and the second a controller. I solved it by disabling all the inputaction and enabling the one of the localplayer:
private void Awake()
{
photonView = GetComponent<PhotonView>();
if (photonView.IsMine)
{
// take control of the user input by disabling all and then enabling mine
var players = FindObjectsOfType<Player>();
foreach (Player player in players)
{
player.GetComponent<PlayerInput>().enabled = false;
}
GetComponent<PlayerInput>().enabled = true;

Unity:Time.timescale =0 not working

I am trying to display player tutorial when the user 1st starts the game using playerpref and want the game to be paused, the problem i am facing is that Time.timescale=0 is not pausing the game when placed inside start (tutorialCanvas gets displayed), but works when is called by a button(Pause button).
Following is the code I use
void Start()
{
if (PlayerPrefs.HasKey ("test4") ==false ) {
tutorialCanvas.SetActive (true);
Time.timeScale = 0;
}
}
Time.timeScale = 0 is really messed up for me so what i could recommend you is that if you just want to pause something like pausing a movement of a character then you can try it like this :
GameObject PlayerScript;
if(Input.GetKey(KeyCode.P)){
//lets disable the playermovement script only not the whole object
PlayerScript = GetComponent<PlayerMovement>().enabled = false;
}
I've been stucked there also so i made an alternative way. And you can also visit this one. If you want a free of that asset you can get it here
I had this issue recently, although it was only doing it on a build, and not in the editor. Changing timeScale to something like 0.0001f would fix it, but that wasn't a great solution. Creating an input settings asset (under Camera -> Player Input -> Open Input Settings) seemed to have fixed it for the builds.

Getting PS3 controller input in Unity3D

I want my PS3 controller to control my unity game, and so far it isn't going well. I have it plugged into my computer via the USB cable it came with. My computer did not install any drivers when I plugged it in, but it did make a sound and Unity seems to have detected it, printing Joystick Connected (controller name here)
Unfortunately, beyond that the controller does absolutely nothing. I have gone to the input menu and have adjusted input according to this map: http://forum.unity3d.com/threads/ps3-button-map.89288/
but still get nothing. Not a single button does anything.
I should also mention if this helps, this is how I'm getting input:
void FixedUpdate()
{
float movehorizontal = Input.GetAxis("Horizontal");
float movevertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(movehorizontal, 0, movevertical);
rb.AddForce(movement * speed);
}
You need a special driver in order to get your PS3 controller to work on PC. I highly recommend ScpToolkit. It's the best one out there.

Categories

Resources