- Home /
I want to change the player's ball velocity by entering transparence3D Objects
I am making unity 3D game that control stages to move a ball to hole of goal, in that game, I made transparence rectangular that when a ball enters in the rectangular, the velocity of ball slower, but when I made a program as shown below, the velocity of ball doesn't change at all. How can I solve this problem? using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WaterPhysicsBehaviour : MonoBehaviour
{
public float VelocityRatioInWater = 0.5f;
public Rigidbody Player;
private void OnTriggerEnter(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
CurrentVelocity = CurrentVelocity * VelocityRatioInWater;
}
}
private void OnTriggerExit(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
CurrentVelocity = CurrentVelocity * 1;
}
}
}
Answer by osuarrei · Sep 21, 2020 at 03:48 PM
CurrentVelocity is likely a copy of Player.Velocity. have you tried setting Player.Velocity?
Answer by unity_ek98vnTRplGj8Q · Sep 21, 2020 at 03:49 PM
You are reading the player's velocity, but never writing to it. Since velocity is just a float value, if you assign it to a variable and change that variable it won't change the original value. Change your code to
private void OnTriggerEnter(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
Player.velocity = CurrentVelocity * VelocityRatioInWater;
}
}
Or, more simply
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Player.velocity *= VelocityRatioInWater;
}
}
Now, I assume you want to set the velocity back to normal when you exit the cube. When this happens your velocity will be the slow value, so you can't just set it to CurrentVelocity * 1, you need to increase the speed. Try
private void OnTriggerExit(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
Player.velocity = CurrentVelocity * (1 / VelocityRatioInWater);
}
}
As what you answer, I now fixed my program$$anonymous$$g, but how can I keep remain slower speed in the slower speed? I just used OntriggerStay, so I want to know other way to remain slower speed except Ontriggerstay. The way to keep slower speed in the transparent rectangular solid.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaterPhysicsBehaviour : $$anonymous$$onoBehaviour
{
public float VelocityRatioInWater = 0.5f;
public Rigidbody Player;
private void OnTriggerEnter(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
Player.velocity = CurrentVelocity * VelocityRatioInWater;
}
}
private void OnTriggerStay(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
Player.velocity = CurrentVelocity * VelocityRatioInWater;
}
}
private void OnTriggerExit(Collider other)
{
var CurrentVelocity = Player.velocity;
if (other.tag == "Player")
{
Player.velocity = CurrentVelocity * (1 / VelocityRatioInWater);
}
}
}
So you are actively controlling the player while he is in the water? You definitely don't want to use OnTriggerStay... can you paste you code that you use to move the player?
I make video games that rotates the stage itself to move ball to put into the goal, therefore there is no specific code for play's ball , but I pan put script for the stages, inspector of the ball and script to control ball's behavior.
Rotating Stage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Transform.rotation example.
// Rotate a GameObject using a Quaternion.
// Tilt the cube using the arrow keys. When the arrow keys are released
// the cube will be rotated back to the center using Slerp.
public class StageRotation : $$anonymous$$onoBehaviour
{
public float smooth = 5.0f;
public float tiltAngle = 60.0f;
void Update()
{
// Smoothly tilts a transform towards a target rotation.
float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle * -1;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle * -1;
// Rotate the cube by converting the angles into a quaternion.
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
// Dampen towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
}
Ball Behaviour
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class PlayerBallBehaviour : $$anonymous$$onoBehaviour
{
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (rb.position.y < -35f)
{
transform.position = new Vector3(-9, 9, 5);
}
var currentYPosition = rb.transform.position.y;
var currentVelocity = rb.velocity;
if (currentYPosition > 35f)
{
currentYPosition = 7f;
currentVelocity.y = -5f;
}
rb.velocity = currentVelocity;
Debug.Log(rb.velocity);
}
}
Inspector of ball