Character starts walking automatically, without any input - unity3d

I have a problem with Unity 4.0.0. When I play the AngryBots project or even when I make my own char with a simple walking script, it automatically walks without pressing any button.
I tried to uninstall Unity, I tried it with an older version of Unity, I even deleted it from the registry, but I still get the same behaviour. Why does that happen? Is it a bug, or am I doing something wrong?
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
} // Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
#script RequireComponent(CharacterController)
I am using this script for all my walking chars and it works, but now its walking automatically.

The issue is something is providing movement axis input.
Generally it will be another device such as joystick etc. First step will be to unplug any other input devices you might have to test (Isolate the problem - then fix as required)
Another thing that may be causing it: I had the same issue but no other input devices plugged in. I found that a driver called VJoy virtual joystick (Installed as part of FreetrackNoIR) was setting movement axis values to 1 and -1 for each axis, thus resulting in the same problem.
Disabling this driver (Control panel, device manager, Human Interface devices) fixed it for me!
Good luck.

You have to use an "Animation" component, not an "Animator" one. (You can assign this by clicking on "Add Component", "Miscellaneous" and "Animation." This will contain the "Play Automatically" button you need.

I had this same thing happen to me. My characters would go UP and LEFT when I would move my mouse off of the Game window while in the editor and in my builds.
Go to "Edit, Project Settings, Input" and verify that your Horizontal and Vertical are set accordingly. Once I deleted the second set of inputs for Horizonal and Vertical in the InputManager, it stopped doing that.

Related

Unity Character flies above the ground whenever I start my game

Every time I start my game the character starts to rise and fly above the ground. I don't understand how to fix it. I've tried to move the ground up but nothing's changed. 2D game if it matters.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller: MonoBehaviour
{
public float speed = 0.1f;
private void Update()
{
transform.position += new Vector3(speed, 0, 0) * Input.GetAxis("Horizontal");
transform.position += new Vector3(0, speed, 0) * Input.GetAxis("Vertical");
if(Input.GetAxis("Horizontal") < 0)//если влево
{
GetComponent<SpriteRenderer>().flipX = true;
}
if(Input.GetAxis("Horizontal") > 0)//если вправо
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}
You should be able to just add the Rigidbody Component to your Character GameObject, then enable gravity in the component and your character should just fall on the ground.
EDIT: If you find your character in the Unity Hierarchy (left hand side of the screen by default) and then to the inspector window (right hand side of the screen by default. At the bottom of the inspector, there should be an ‘Add Component’ button. Click that button and then search ‘Rigidbody’ in the search field that appears. Add the Rigidbody Component. By default, the enable gravity option should be ticked but if it isn’t, just make sure you tick that box. When you run your game with gravity enabled, gravity should act on the GameObject with the ‘Rigidbody’ Component, pulling it to the ground
Also, make sure that you follow the same process as above to add the ‘Mesh Collider’ Component to your platform/ground - this will make your ground a ‘Physical’ object which will prevent your character falling straight through it
Hope this helps - let me know if it doesn’t work

Player stops move the character direction resets [Unity 2D]

My character is a car and I try to rotate it the direction it move, so far so good I succeeded to do that but once I stop moving the character flips back to the direction it was on the start.
Also how can I make my turns from side to the opposite site smooth ?
Here is my code so far:
[SerializeField] float driveSpeed = 5f;
//state
Rigidbody2D myRigidbody;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
//Control of velocity of the car
float HorizontalcontrolThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // Value between -1 to 1
float VerticalcontrolThrow = CrossPlatformInputManager.GetAxis("Vertical"); // Value between -1 to 1
Vector2 playerVelocity = new Vector2(HorizontalcontrolThrow * driveSpeed, VerticalcontrolThrow * driveSpeed);
myRigidbody.velocity =playerVelocity;
**//Direction of the car**
Vector2 direction = new Vector2(HorizontalcontrolThrow, VerticalcontrolThrow);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
myRigidbody.rotation = angle;
}
I'm not sure about this, but maybe that last line "myRigidbody.rotation = angle" being called every frame is what is making your car reset its rotation.
Maybe change it to "myRigidbody.rotation *= angle" or "myRigidbody.rotation += angle".
It looks like it may be because HorizontalcontrolThrow and VerticalcontrolThrow are going to be reset when you release the controls. If it's resetting to its original orientation, then what's happening is that until you move, those two values are going to be at their default value. You then move and it affects the rotation. But when you release the controls, those values are back to the starting values again, and so is your rotation.
What you therefore need to do is try to separate the HorizontalcontrolThrow and VerticalcontrolThrow from the rest of the code, which should only be activated when at least one of these two variables are not at their default setting (I can't remember what the axis functions return at the moment).
Edit:
An IF statement should suffice (some rough pseudo code):
if (horizontalAxis != default || verticalAxis != default)
{
Rotate/Move
}
I solved the snap rotation using Quaternion at rotation, the issiu I had with it was to convert it from 3d to 2d, through the guide of this clip: youtube.com/watch?v=mKLp-2iseDc and made my adjustments it works just fine !

How can I disable orientation with Oculus Locomotion Controller?

I am new to Unity and VR. I am working on a VR app using the Oculus Quest. I have set up a teleportation system based on the following tutorial video by Valem: https://www.youtube.com/watch?v=r1kF0PhwQ8E - but find that when I turn my head or use the thumbstick to oriented during teleportation, after landing I can be facing sideways and any further movement using the thumbstick appears to be incorrectly aligned - so when you try to move forwards you move sideways etc.
I presume my camera rig (which is a child of the OVRPlayerController) is facing the wrong way. This is backed up by the fact that some text I've placed in front of the rig appears to have shifted to the side instead of straight up front.
I just want a simple teleportation system so you move to the new location but do not orientate - instead you remain facing in the same direction as you started.
Is it possible to disable orientation? Or are there any recommended alternative solutions for a teleportation system?
i used this workaround to remove teleport orientation:
open TeleportDestination prefab
delete the orientation gameobject
hit play, then fix those scripts that referenced it
you'll also end up inside LocomotionTeleport.cs DoTeleport(), where it seems to set that teleport rotation (so probably enough if modify code here)
public void DoTeleport()
{
var character = LocomotionController.CharacterController;
var characterTransform = character.transform;
// removed orientation
//var destTransform = _teleportDestination.OrientationIndicator;
var destTransform = _teleportDestination.transform;
Vector3 destPosition = destTransform.position;
destPosition.y += character.height * 0.5f;
// removed orientation
//Quaternion destRotation = _teleportDestination.LandingRotation;// destTransform.rotation;
#if false
Quaternion destRotation = destTransform.rotation;
//Debug.Log("Rots: " + destRotation + " " + destTransform.rotation * Quaternion.Euler(0, -LocomotionController.CameraRig.trackingSpace.localEulerAngles.y, 0));
destRotation = destRotation * Quaternion.Euler(0, -LocomotionController.CameraRig.trackingSpace.localEulerAngles.y, 0);
#endif
if (Teleported != null)
{
//Teleported(characterTransform, destPosition, destRotation);
Teleported(characterTransform, destPosition, characterTransform.rotation);
}
characterTransform.position = destPosition;
//characterTransform.rotation = destRotation;
}

Unity2D: currentSelectedGameObject panel disabled

Okay, so I have a click to move game and a panel in which I do not want my player to move towards. However the panel is disabled at first (for an effect I'm doing) until the user clicks on a button to SetActive the panel. I used currentSelectedGameObject to block my player going to towards the panel but it isn't working, maybe because the panel was disabled in the first place, I not sure just spitting out ideas. Hopeful someone can help me.
using UnityEngine.EventSystems;
public GameObject currentSelectedGameObject;
public void Update () {
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.currentSelectedGameObject)
return;
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.fixedDeltaTime);
}
Thank you. :)
Use the button to setactive the panel and if not , you are not using the public GameObject currentSelectedGameObject variable in your code , do this :
if (EventSystem.current.currentSelectedGameObject == currentSelectedGameObject) // now this is your variable that you declared being used in the if statement
I cant remember correctly but mb its like this EventSystem.current.currentSelectedGameObject.name

Unity Navmeshagent won't face the target object when reaches stopping distance

I'm trying to get the NPC to look at the main character when I'm talking to him. I need to make sure it looks natural and that he is facing me. I know I can do Transform.LookAt() but that is too instant and unnatural.
How do I rotate the navmeshagent to face my character when its stopped moving?
Try this out to control the body orientation - the slerp is adjustable to your desired rotation speed (https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html):
private void FaceTarget(Vector3 destination)
{
Vector3 lookPos = destination - transform.position;
lookPos.y = 0;
Quaternion rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, [fill in desired rotation speed]);
}
if(agent.remainingDistance < agent.stoppingDistance)
{
agent.updateRotation = false;
//insert your rotation code here
}
else {
agent.updateRotation = true;
}
This will rotate your agent when it's distance below the stoppingDistance variable. However it will look inhuman, so if you're going for a humanoid ai I'd recommend looking at the unity Mecanim demo (particularly the locomotion scene) as it has code and animations that will properly animate the agent.
Maybe try this Head Look Controller. It's very smooth and blends with animations!
Put the char in a game object and copy the navmesh from the char to the parent, uncheck enable in char. move any scripts up too.
Just spent 5 hours to find this.